//
//  ExamQuestionsViewController.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/3.
//

#import "ExamQuestionsViewController.h"
#import "ExamQuestionsCollectionViewCell.h"
#import "ExamQuestionModel.h"
#import "TestSubmitAlertView.h"
#import "ExamAnswerSheetView.h"
#import "ExamTimeCountDownView.h"
#import "TestQuitAlertView.h"
#import "ErrorCorrectionViewController.h"
#import "TestListenAlertView.h"
#import "ExamPaperQuestionManager.h"
#import "ExamQuestionOptionsModel.h"
#import "ExamUpdateQuestionManager.h"
#import "ExamPaperAnswerSheetManager.h"
#import "ExamPaperAnswerSheetModel.h"
#import "ExamPaperAnswerSheetListModel.h"
#import "ExamPaperAnswerSheetRecordModel.h"
#import "ExamQuestionReadCollectionViewCell.h"
#import "ExamQuestionWriteCollectionViewCell.h"
#import "CollectSaveManager.h"
#import "WordCollectDeleteManager.h"
#import "ExamCommitExamManager.h"
#import "ExamQuestionReadListModel.h"
#import "ExamQuestionResultManager.h"
#import "ExamResultAnswerSheetManager.h"

@interface ExamQuestionsViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,JPAPIManagerCallBackDelegate,ExamQuestionsCollectionViewCellDelegate,ExamQuestionReadCollectionViewCellDelegate>

@property(nonatomic,strong)UILabel *countLabel;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSMutableArray *answerArr;
@property(nonatomic,strong)UIButton *commitBtn;
@property(nonatomic,strong)TestSubmitAlertView *submitAlertView;
@property(nonatomic,strong)UIButton *onlyErrorBtn;
@property(nonatomic,strong)ExamAnswerSheetView *answerSheetView;
@property(nonatomic,strong)ExamTimeCountDownView *countDownView;
@property (nonatomic,strong)NSTimer *timer;//试卷倒计时
@property(nonatomic,assign)NSInteger seconds;
@property(nonatomic,assign)NSInteger totalSeconds;
@property(nonatomic,strong)UIImageView *listenImageView;
@property(nonatomic,strong)TestQuitAlertView *quitAlertView;
@property(nonatomic,strong)TestListenAlertView *listenAlertView;
@property(nonatomic,strong)TestSubmitAlertView *wordSubmitAlertView;
@property(nonatomic,strong)TestSubmitAlertView *listenSubmitAlertView;
@property(nonatomic,strong)ExamPaperQuestionManager *questionManager;
@property(nonatomic,strong)ExamUpdateQuestionManager *updateQuestionManager;
@property(nonatomic,strong)ExamPaperAnswerSheetManager *answerSheetManager;
@property(nonatomic,strong)NSMutableArray *answerSheetArr;
@property(nonatomic,strong)NSMutableArray *listenAnswerSheetArr;
@property(nonatomic,copy)NSString *answerSheetCount;
@property(nonatomic,assign)NSInteger listen_count;//考试时听力答题卡题目数量
@property(nonatomic,strong)NSTimer *listenTimer;//听力倒计时
@property(nonatomic,assign)BOOL isTestListen;//是否是开始听力测试
@property(nonatomic,strong)AVPlayer *avPlayer;
@property(nonatomic,strong)AVPlayerItem *currentItem;//听筒当前item
@property (nonatomic, assign) BOOL isPlaying;
@property(nonatomic,assign)BOOL isFinish;//是否完成所有题目
@property(nonatomic,strong)UIButton *collectBtn;
@property(nonatomic,strong)CollectSaveManager *collectSaveManager;
@property(nonatomic,strong)ExamQuestionModel *selQuestionModel;//当前选中的问题
@property(nonatomic,assign)NSInteger big_question_index;
@property(nonatomic,strong)WordCollectDeleteManager *collectDeleteManager;
@property(nonatomic,assign)NSInteger sel_index;//选中的问题下标
@property(nonatomic,assign)NSInteger totalCount;//总共要测试的题目个数
@property(nonatomic,assign)NSInteger listenCount;//听力题目个数
@property(nonatomic,strong)ExamQuestionReadCollectionViewCell *readCell;
@property(nonatomic,strong)ExamQuestionsCollectionViewCell *selCell;
@property(nonatomic,strong)ExamCommitExamManager *commitExamManager;
@property(nonatomic,assign)BOOL isHaveListen;//是否有听力
@property(nonatomic,copy)NSString *commitTipStr;//考试前半部分提交的提示语文本
@property(nonatomic,strong)NSMutableArray *listenArr;
@property(nonatomic,strong)ExamQuestionResultManager *questionResultManager;
@property(nonatomic,strong)ExamResultAnswerSheetManager *resultAnswerSheetManager;
@property(nonatomic,copy)NSString *currentHearingFile;//当前听力文件
@property(nonatomic,strong)ExamQuestionModel *selBigQuestionModel;//当前选中的大题问题
@property(nonatomic,strong)JPProgressHUD *loadingHub;
@property(nonatomic,strong)JPProgressHUD *commitHub;
@property(nonatomic,strong)UIView *topView;
@property(nonatomic,strong)UIView *bottomView;

@end

@implementation ExamQuestionsViewController

-(NSMutableArray *)dataArr{
    if(!_dataArr){
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(NSMutableArray *)answerArr{
    if(!_answerArr){
        _answerArr = [NSMutableArray array];
    }
    return _answerArr;
}

-(NSMutableArray *)answerSheetArr{
    if(!_answerSheetArr){
        _answerSheetArr = [NSMutableArray array];
    }
    return _answerSheetArr;
}

-(NSMutableArray *)listenAnswerSheetArr{
    if(!_listenAnswerSheetArr){
        _listenAnswerSheetArr = [NSMutableArray array];
    }
    return _listenAnswerSheetArr;
}

-(NSMutableArray *)listenArr{
    if(!_listenArr){
        _listenArr = [NSMutableArray array];
    }
    return _listenArr;
}

-(ExamPaperQuestionManager *)questionManager{
    if(!_questionManager){
        _questionManager = [[ExamPaperQuestionManager alloc]init];
        _questionManager.delegate = self;
    }
    return _questionManager;
}

-(ExamUpdateQuestionManager *)updateQuestionManager{
    if(!_updateQuestionManager){
        _updateQuestionManager = [[ExamUpdateQuestionManager alloc]init];
        _updateQuestionManager.delegate = self;
    }
    return _updateQuestionManager;
}

-(ExamPaperAnswerSheetManager *)answerSheetManager{
    if(!_answerSheetManager){
        _answerSheetManager = [[ExamPaperAnswerSheetManager alloc]init];
        _answerSheetManager.delegate = self;
    }
    return _answerSheetManager;
}

-(CollectSaveManager *)collectSaveManager{
    if(!_collectSaveManager){
        _collectSaveManager = [[CollectSaveManager alloc]init];
        _collectSaveManager.delegate = self;
    }
    return _collectSaveManager;
}

-(WordCollectDeleteManager *)collectDeleteManager{
    if(!_collectDeleteManager){
        _collectDeleteManager = [[WordCollectDeleteManager alloc]init];
        _collectDeleteManager.delegate = self;
    }
    return _collectDeleteManager;
}

-(ExamCommitExamManager *)commitExamManager{
    if(!_commitExamManager){
        _commitExamManager = [[ExamCommitExamManager alloc]init];
        _commitExamManager.delegate = self;
    }
    return _commitExamManager;
}

-(ExamQuestionResultManager *)questionResultManager{
    if(!_questionResultManager){
        _questionResultManager = [[ExamQuestionResultManager alloc]init];
        _questionResultManager.delegate = self;
    }
    return _questionResultManager;
}

-(ExamResultAnswerSheetManager *)resultAnswerSheetManager{
    if(!_resultAnswerSheetManager){
        _resultAnswerSheetManager = [[ExamResultAnswerSheetManager alloc]init];
        _resultAnswerSheetManager.delegate = self;
    }
    return _resultAnswerSheetManager;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self popGestureClose];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self popGestureOpen];
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    [self listentTypeSet];
    
    self.big_question_index = 0;
    self.totalCount = 0;
    self.isHaveListen = NO;
    if(self.testType == 1||self.testType == 2){
        if(self.typeArr){
            NSMutableArray *arr = [NSMutableArray array];
            for(NSInteger i = 0;i < self.typeArr.count;i++){
                ExamPaperTypeListModel *model = self.typeArr[i];
                if([model.typeId isEqualToString:@"33"]){
                    self.isHaveListen = YES;
                }
                if (!([model.typeId isEqualToString:@"36"]||[model.typeId isEqualToString:@"33"])){
                    [arr addObject:model.typeName];
                }
            }
            if(self.testType == 1){
                if(arr.count == 1){
                    self.commitTipStr = arr[0];
                }else if (arr.count == 2){
                    self.commitTipStr = [NSString stringWithFormat:@"%@和%@",arr[0],arr[1]];
                }else if (arr.count > 2){
                    NSMutableString *str = [NSMutableString string];
                    for(NSInteger j = 0;j < arr.count;j++){
                        NSString *tipstr = arr[j];
                        [str appendString:tipstr];
                        if(j == arr.count-2){
                            [str appendString:@"和"];
                        }else if (j == arr.count-1){
                        }else{
                            [str appendString:@","];
                        }
                    }
                    self.commitTipStr = str;
                    
                }
            }
        }
    }
    
    
    [self createUI];
    
    self.loadingHub = [JPProgressHUD showLoadingInView:self.view];
    if(self.testType == 2||self.testType == 3){
        //获取试卷解析
        [self loadQuestionResultDataWithCorrect:@""];
    }else{
        [self loadQuestionData];
    }
    
    //获取答题卡数据
    [self loadAnswerSheetDataWithIsFirst:YES];
    
    //听力播放结束
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playToEndTime:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    
}

#pragma mark - 设置静音模式下也能播放音频
-(void)listentTypeSet{
    // 获取当前的音频会话类别
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    // 检查当前类别是否已经是 AVAudioSessionCategoryPlayback
    if (![audioSession.category isEqualToString:AVAudioSessionCategoryPlayback]) {
        NSError *setCategoryError = nil;
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
        
        if (setCategoryError) {
            NSLog(@"Error setting category: %@", [setCategoryError localizedDescription]);
        }
    }
    
    // 检查音频会话是否已经处于激活状态
    if (!audioSession.isOtherAudioPlaying) {
        NSError *activationError = nil;
        [audioSession setActive:YES error:&activationError];
        
        if (activationError) {
            NSLog(@"Error activating session: %@", [activationError localizedDescription]);
        }
    }
}

#pragma mark - 获取专项测试数据
-(void)loadQuestionData{
    self.questionManager.paperId = self.paperModel.paperId;
    if(self.testType == 0||self.testType == 3){
        self.questionManager.type = self.typeModel?self.typeModel.ptId:@"";
    }else{
        self.questionManager.type = @"";
    }
    [self.questionManager loadData];
}

#pragma mark - 获取考试结果数据
-(void)loadQuestionResultDataWithCorrect:(NSString*)correct{
    [self.dataArr removeAllObjects];
    self.questionResultManager.paperId = self.paperModel.paperId;
    self.questionResultManager.correct = correct;
    if(self.testType == 3){
        //专项结果
        self.questionResultManager.ptId = self.typeModel.ptId;
    }else{
        self.questionResultManager.ptId = @"";
    }
    [self.questionResultManager loadData];
}

#pragma mark - 答题
-(void)answerQustionWithQuestionId:(NSString*)questionId andAnswer:(NSString*)answer andIsDone:(NSString*)isDone{
    self.updateQuestionManager.questionId = questionId;
    self.updateQuestionManager.answer = answer;
    NSString *type = (self.testType == 0)?@"2":@"1";
    self.updateQuestionManager.type = type;
    self.updateQuestionManager.isDone = isDone;
    [self.updateQuestionManager loadData];
}

#pragma mark - 交卷
-(void)commitWithPtId:(NSString*)ptId{
    self.commitHub = [JPProgressHUD JPShowHUDWithMsg:@"正在交卷，请不要离开..." inView:self.view];
    self.commitExamManager.paperId = self.paperModel.paperId;
    self.commitExamManager.ptId = ptId;
    [self.commitExamManager loadData];
}

#pragma mark - 创建界面
-(void)createUI{
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = JPUIFontInterMedium(17);
    titleLabel.textColor = KFontBlackColor;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = self.paperModel.name;
    [self.customNavigationBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@60);
        make.right.mas_equalTo(@-60);
        make.height.mas_equalTo(@30);
        make.bottom.mas_equalTo(@-7);
    }];
    
    self.customNavigationBar.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = UIColorHex(0xF2F7F6);
    
    if(self.testType == 0||self.testType == 1){
        self.commitBtn = [[UIButton alloc]init];
        if(self.testType == 0){
            [self.commitBtn setTitle:@"提交" forState:UIControlStateNormal];
        }else{
            [self.commitBtn setTitle:@"交卷" forState:UIControlStateNormal];
        }
        [self.commitBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
        self.commitBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
        self.commitBtn.ym_acceptEventInterval = 2.0;
        [self.commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.customNavigationBar addSubview:self.commitBtn];
        CGFloat w = [self getStringWidthWithText:self.commitBtn.titleLabel.text font:self.commitBtn.titleLabel.font viewHeight:32];
        [self.commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(@-6);
            make.width.mas_equalTo(w+20);
            make.height.mas_equalTo(@32);
            make.bottom.mas_equalTo(@-6);
        }];
        if(self.testType == 0&&self.typeModel.typeId&&self.typeModel.typeId.integerValue == 55){
            //阅读b不显示提交按钮
            self.commitBtn.hidden = YES;
        }else{
            self.commitBtn.hidden = NO;
        }
    }
   
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0,kYM_NAVIGATIONBAR_HEIGHT,self.currentAvailableWidth,44)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    
    self.countLabel = [[UILabel alloc]init];
    self.countLabel.textColor = KFontGrayColor;
    self.countLabel.font = JPUIFontInterRegular(15);
    self.countLabel.textAlignment = NSTextAlignmentRight;
    self.countLabel.text = @"0/0";
    [topView addSubview:self.countLabel];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@-16);
        make.height.mas_equalTo(@18);
        make.centerY.mas_equalTo(topView);
    }];
    
    self.onlyErrorBtn = [[UIButton alloc]init];
    [self.onlyErrorBtn setTitle:@" 只看错题 " forState:UIControlStateNormal];
    [self.onlyErrorBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
    self.onlyErrorBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    [self.onlyErrorBtn setImage:[UIImage imageNamed:@"ohly_error_unsel"] forState:UIControlStateNormal];
    [self.onlyErrorBtn setImage:[UIImage imageNamed:@"ohly_error_sel"] forState:UIControlStateSelected];
    self.onlyErrorBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//左
    [self.onlyErrorBtn addTarget:self action:@selector(onlyErrorBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:self.onlyErrorBtn];
    CGFloat o_w = [self getStringWidthWithText:self.onlyErrorBtn.titleLabel.text font:self.onlyErrorBtn.titleLabel.font viewHeight:32];
    [self.onlyErrorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.width.mas_equalTo(o_w+18);
        make.height.mas_equalTo(@32);
        make.centerY.mas_equalTo(topView);
    }];
    
    self.listenImageView = [[UIImageView alloc]init];
    self.listenImageView.image = [UIImage imageNamed:@"test_voice_play"];
    self.listenImageView.userInteractionEnabled = YES;
    [topView addSubview:self.listenImageView];
    [self.listenImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@20);
        make.centerX.mas_equalTo(topView);
        make.centerY.mas_equalTo(topView);
    }];
    UITapGestureRecognizer *listenTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(listenPlayTapGesture)];
    [self.listenImageView addGestureRecognizer:listenTap];
    
    if(self.testType == 2||self.testType == 3){
        self.onlyErrorBtn.hidden = NO;
        if(self.typeModel&&[self.typeModel.typeId isEqualToString:@"33"]){
            //听力
            self.listenImageView.hidden = NO;
        }else{
            if(self.isHaveListen == YES){
                self.listenImageView.hidden = NO;
            }else{
                self.listenImageView.hidden = YES;
            }
        }
        if(self.testType == 2){
            self.listenImageView.hidden = YES;
        }
    }else{
        self.onlyErrorBtn.hidden = YES;
        if(self.testType == 1){
            //试卷
            self.onlyErrorBtn.hidden = YES;
            self.countDownView = [[ExamTimeCountDownView alloc]initWithFrame:CGRectMake(16,10,63,24)];
            [topView addSubview:self.countDownView];
            _seconds = 0;
            if(self.paperModel.time){
                if(self.isHaveListen == YES){
                    _totalSeconds = (self.paperModel.time.integerValue-self.paperModel.hearingTime.integerValue)*60;
                }else{
                    _totalSeconds = self.paperModel.time.integerValue*60;
                }
                NSInteger hour = _totalSeconds/3600;
                NSInteger min = (_totalSeconds - hour*3600)/60;
                [self.countDownView refreshWithHour:[NSString stringWithFormat:@"%02ld",(long)hour] andMinute:[NSString stringWithFormat:@"%02ld",(long)min]];
                if(!self.timer){
                    self.timer = [NSTimer timerWithTimeInterval:60.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
                    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
                }else{
                    //开启定时器
                    [self.timer setFireDate:[NSDate distantPast]];
                }


            }
            self.listenImageView.hidden = YES;
        }else{
            if(self.typeModel&&[self.typeModel.typeId isEqualToString:@"33"]){
                //听力
                self.listenImageView.hidden = NO;
            }else{
                self.listenImageView.hidden = YES;
            }
        }
    }
    
    self.topView = topView;

    [self createCollecionView];
    
    __weak typeof(self)self_weak_ = self;
    UIView *bottomView = [[UIView alloc]init];
    bottomView.backgroundColor = [UIColor whiteColor];
    bottomView.layer.cornerRadius = 58/2.0;
    bottomView.layer.shadowColor = UIColorHex_(0x000000,0.25).CGColor;
    bottomView.layer.shadowOffset = CGSizeMake(0,0);
    bottomView.layer.shadowOpacity = 1;
    bottomView.layer.shadowRadius = 10;
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@256);
        make.height.mas_equalTo(@58);
        make.centerX.mas_equalTo(self_weak_.view);
        make.bottom.mas_equalTo(@-35);
    }];
    
    NSArray *imageArr = @[@"exam_question_uncollect",@"exam_question_answer_sheet",@"exam_question_error_list"];
    for (NSInteger i = 0; i < imageArr.count; i++){
        UIButton *btn = [[UIButton alloc]init];
        [btn setImage:[UIImage imageNamed:imageArr[i]] forState:UIControlStateNormal];
        if(i == 0){
            [btn setImage:[UIImage imageNamed:@"exam_question_collect"] forState:UIControlStateSelected];
            btn.selected = NO;
        }
        btn.ym_acceptEventInterval = 1.0;
        btn.tag = 666+i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(@44);
            if(i == 0){
                make.left.mas_equalTo(@22);
            }else if ( i == 1){
                make.centerX.mas_equalTo(bottomView);
            }else{
                make.right.mas_equalTo(@-22);
            }
            make.centerY.mas_equalTo(bottomView);
        }];
        if(i == 0){
            self.collectBtn = btn;
        }
    }
       
    self.bottomView = bottomView;
    
}

#pragma mark - 创建collectionview
-(void)createCollecionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.topView.frame), self.currentAvailableWidth,self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-44) collectionViewLayout:layout];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollEnabled = YES;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.backgroundColor = UIColorHex(0xF2F7F6);
    [self.collectionView registerClass:NSClassFromString(@"ExamQuestionsCollectionViewCell") forCellWithReuseIdentifier:@"ExamQuestionsCollectionViewCell"];
    [self.collectionView registerClass:NSClassFromString(@"ExamQuestionReadCollectionViewCell") forCellWithReuseIdentifier:@"ExamQuestionReadCollectionViewCell"];
    [self.collectionView registerClass:NSClassFromString(@"ExamQuestionWriteCollectionViewCell") forCellWithReuseIdentifier:@"ExamQuestionWriteCollectionViewCell"];
    [self.collectionView registerClass:NSClassFromString(@"UICollectionViewCell") forCellWithReuseIdentifier:@"UICollectionViewCell"];
    [self.view addSubview:self.collectionView];
    
}

#pragma mark - 屏幕宽度改变
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    if(isIPAD){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 获取当前可用宽度并更新布局
            self.currentAvailableWidth = [DeviceUtils currentAvailableWidth];
            self.currentAvailableHeight = [DeviceUtils currentAvailableHeight];
            self.customNavigationBar.frame = CGRectMake(0, 0, self.currentAvailableWidth,kYM_NAVIGATIONBAR_HEIGHT);
            self.topView.frame = CGRectMake(0,kYM_NAVIGATIONBAR_HEIGHT,self.currentAvailableWidth,44);
            [self.collectionView removeFromSuperview];
            self.collectionView = nil;
            [self createCollecionView];
            [self.collectionView reloadData];
            [self.collectionView setContentOffset:CGPointMake(self.big_question_index*self.currentAvailableWidth,0) animated:NO];
            [self.view bringSubviewToFront:self.bottomView];
            if(self.submitAlertView){
                self.submitAlertView.frame = CGRectMake(0, 0,self.currentAvailableWidth, self.currentAvailableHeight);
                self.submitAlertView.currentAvailableWidth = self.currentAvailableWidth;
                self.submitAlertView.currentAvailableHeight = self.currentAvailableHeight;
                [self.submitAlertView refreshWidth];
            }
            
            if(self.listenSubmitAlertView){
                self.listenSubmitAlertView.frame = CGRectMake(0, 0,self.currentAvailableWidth, self.currentAvailableHeight);
                self.listenSubmitAlertView.currentAvailableWidth = self.currentAvailableWidth;
                self.listenSubmitAlertView.currentAvailableHeight = self.currentAvailableHeight;
                [self.listenSubmitAlertView refreshWidth];
            }
            
            if(self.wordSubmitAlertView){
                self.wordSubmitAlertView.frame = CGRectMake(0, 0,self.currentAvailableWidth, self.currentAvailableHeight);
                self.wordSubmitAlertView.currentAvailableWidth = self.currentAvailableWidth;
                self.wordSubmitAlertView.currentAvailableHeight = self.currentAvailableHeight;
                [self.wordSubmitAlertView refreshWidth];
            }
            
            if(self.listenAlertView){
                self.listenAlertView.frame = CGRectMake(0, 0, self.currentAvailableWidth, self.currentAvailableHeight);
            }
            
            if(self.quitAlertView){
                self.quitAlertView.frame = CGRectMake(0, 0, self.currentAvailableWidth, self.currentAvailableHeight);
            }
            
            if(self.answerSheetView){
                self.answerSheetView.frame = CGRectMake(0, 0, self.currentAvailableWidth,self.currentAvailableHeight);
                self.answerSheetView.currentAvailableWidth = self.currentAvailableWidth;
                self.answerSheetView.currentAvailableHeight = self.currentAvailableHeight;
                [self.answerSheetView refreshWidth];
            }
            
        });
    }
}


#pragma mark - 提交
-(void)commitBtnClick{
    if((self.answerArr.count > 0)&&(self.answerArr.count == self.totalCount)){
      if(self.testType == 0){
          //测试
        [self showTestCommitAlertView];
      }else if (self.testType == 1){
          if(self.isTestListen == NO){
              //选择题交卷提示框
              [self showWordCommitAlertView];
          }else{
              //听力测试完试卷提交
              [self showListenCommitAlertView];
          }
      }
    }else{
        [JPProgressHUD showMsg:@"请完成所有题目再提交"];
    }
    
}

#pragma mark - 创建听力倒计时
-(void)createListenTestTimer{
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    _seconds = 0;
    if(self.paperModel.hearingTime){
        _totalSeconds = self.paperModel.hearingTime.integerValue*60;
        NSInteger hour = _totalSeconds/3600;
        NSInteger min = (_totalSeconds - hour*3600)/60;
        [self.countDownView refreshWithHour:[NSString stringWithFormat:@"%02ld",(long)hour] andMinute:[NSString stringWithFormat:@"%02ld",(long)min]];
        if(!self.listenTimer){
            //一分钟一次
            self.listenTimer = [NSTimer timerWithTimeInterval:60.0 target:self selector:@selector(updateListenTime) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:self.listenTimer forMode:NSDefaultRunLoopMode];
        }else{
            //开启定时器
            [self.listenTimer setFireDate:[NSDate distantPast]];
        }
    }
}

#pragma mark - 听力时间倒计时
-(void)updateListenTime{
    _seconds = _seconds+60.0;
    if(_seconds < _totalSeconds){
        NSInteger hour = (_totalSeconds - _seconds)/3600;
        NSInteger min = (_totalSeconds - _seconds - hour*3600)/60;
        [self.countDownView refreshWithHour:[NSString stringWithFormat:@"%02ld",(long)hour] andMinute:[NSString stringWithFormat:@"%02ld",(long)min]];
    }else{
        //计时结束
        if(self.listenTimer){
            [self.listenTimer invalidate];
            self.listenTimer = nil;
        }
        if([[self currentViewController] isKindOfClass:[ExamQuestionsViewController class]]){
            //自动交卷
            [JPProgressHUD showMsg:@"考试时间到，自动为您交卷"];
            [self commitWithPtId:@""];
        }
    }
}

#pragma mark - 提交练习提示框
-(void)showTestCommitAlertView{
    if(!self.submitAlertView){
        self.submitAlertView = [[TestSubmitAlertView alloc]initWithFrame:CGRectMake(0, 0, self.currentAvailableWidth,self.currentAvailableHeight) andTitle:[NSString stringWithFormat:@"是否提交%@练习?本次答题情况将纳入统计。",self.typeModel?self.typeModel.typeName:@""] andLeftTitle:@"交卷" andleftColor:UIColorHex(0xFE4A4B) andRightTitle:@"继续考试" andRightColor:KFontGrayColor andTitleFont:JPUIFontPingFangSCRegular(16) andLeftFont:JPUIFontPingFangSCRegular(16) andRightFont:JPUIFontPingFangSCRegular(16)];
    }
    self.submitAlertView.currentAvailableWidth = self.currentAvailableWidth;
    self.submitAlertView.currentAvailableHeight = self.currentAvailableHeight;
    UIWindow *keyWindow = KEY_WINDOW;
    if(keyWindow){
        [keyWindow addSubview:self.submitAlertView];
    }
    __weak typeof(self)self_weak_ = self;
    [self.submitAlertView setLeftBlock:^{
        [self_weak_.submitAlertView removeFromSuperview];
         //提交
        [self_weak_ commitWithPtId:self_weak_.typeModel.ptId];
    }];
    [self.submitAlertView setRightBlock:^{
        [self_weak_.submitAlertView removeFromSuperview];
    }];
  
}

#pragma mark - 听力交卷提示框
-(void)showListenCommitAlertView{
    
    if(!self.listenSubmitAlertView){
        self.listenSubmitAlertView = [[TestSubmitAlertView alloc]initWithFrame:CGRectMake(0, 0, self.currentAvailableWidth,self.currentAvailableHeight) andTitle:@"是否交卷听力板块？" andLeftTitle:@"交卷" andleftColor:UIColorHex(0xFE4A4B) andRightTitle:@"继续考试" andRightColor:KFontGrayColor andTitleFont:JPUIFontPingFangSCRegular(16) andLeftFont:JPUIFontPingFangSCRegular(16) andRightFont:JPUIFontPingFangSCRegular(16)];
    }
    self.listenSubmitAlertView.currentAvailableWidth = self.currentAvailableWidth;
    self.listenSubmitAlertView.currentAvailableHeight = self.currentAvailableHeight;
    UIWindow *keyWindow = KEY_WINDOW;
    if(keyWindow){
        [keyWindow addSubview:self.listenSubmitAlertView];
    }
    __weak typeof(self)self_weak_ = self;
    [self.listenSubmitAlertView setLeftBlock:^{
        [self_weak_.listenSubmitAlertView removeFromSuperview];
        if(self_weak_.listenTimer){
            [self_weak_.listenTimer invalidate];
            self_weak_.listenTimer = nil;
        }
        if(self_weak_.avPlayer){
            [self_weak_.avPlayer pause];
        }
        //听力结束再交卷
        [self_weak_ commitWithPtId:@""];
    }];
    [self.listenSubmitAlertView setRightBlock:^{
        [self_weak_.listenSubmitAlertView removeFromSuperview];
    }];
}

#pragma mark - 选择题交卷提示框
-(void)showWordCommitAlertView{
    if(!self.wordSubmitAlertView){
        self.wordSubmitAlertView = [[TestSubmitAlertView alloc]initWithFrame:CGRectMake(0, 0, self.currentAvailableWidth,self.currentAvailableHeight) andTitle:[NSString stringWithFormat:@"是否交卷%@板块？",self.commitTipStr] andLeftTitle:@"交卷" andleftColor:UIColorHex(0xFE4A4B) andRightTitle:@"继续考试" andRightColor:KThemeColor andTitleFont:JPUIFontPingFangSCRegular(16) andLeftFont:JPUIFontPingFangSCRegular(16) andRightFont:JPUIFontPingFangSCRegular(16)];
    }
    self.wordSubmitAlertView.currentAvailableWidth = self.currentAvailableWidth;
    self.wordSubmitAlertView.currentAvailableHeight = self.currentAvailableHeight;
    UIWindow *keyWindow = KEY_WINDOW;
    if(keyWindow){
        [keyWindow addSubview:self.wordSubmitAlertView];
    }
    __weak typeof(self)self_weak_ = self;
    [self.wordSubmitAlertView setLeftBlock:^{
        [self_weak_.wordSubmitAlertView removeFromSuperview];
        if(self_weak_.isHaveListen == YES){
            [self_weak_ showListenAlertView];
        }else{
            //没有听力 直接交卷
            [self_weak_ commitWithPtId:@""];
        }
    }];
    [self.wordSubmitAlertView setRightBlock:^{
        [self_weak_.wordSubmitAlertView removeFromSuperview];
    }];
}

#pragma mark - 听力时间提示框
-(void)showListenAlertView{
    if(!self.listenAlertView){
        self.listenAlertView = [[TestListenAlertView alloc]initWithFrame:CGRectMake(0, 0,self.currentAvailableWidth,self.currentAvailableHeight)];
    }
    UIWindow *window = KEY_WINDOW;
    if(window){
        [window addSubview:self.listenAlertView];
        __weak typeof(self)self_weak_ = self;
        [self.listenAlertView setKnowBlock:^{
            [self_weak_.listenAlertView removeFromSuperview];
            //开始听力考试
            [self_weak_.commitBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
            self_weak_.listenImageView.hidden = NO;
            self_weak_.isTestListen = YES;
            [self_weak_.answerArr removeAllObjects];
            self_weak_.dataArr = [NSMutableArray arrayWithArray:self_weak_.listenArr];
            self_weak_.big_question_index = 0;
            self_weak_.totalCount = self_weak_.listenCount;
            self_weak_.countLabel.text = [NSString stringWithFormat:@"0/%ld",self_weak_.totalCount];
            [self_weak_.collectionView reloadData];
            [self_weak_.collectionView setContentOffset:CGPointMake(0,0)];
            self_weak_.answerSheetArr = [NSMutableArray arrayWithArray:self_weak_.listenAnswerSheetArr];
            self_weak_.answerSheetCount = [NSString stringWithFormat:@"%ld",self_weak_.listen_count];
            [self_weak_ createListenTestTimer];
            self_weak_.isPlaying = NO;
            [self_weak_ playListenWithHearingFile:self_weak_.paperModel.hearingFile?self_weak_.paperModel.hearingFile:@""];
        }];
        
    }
    
}

#pragma mark - 返回
-(void)defaultBackSet{
    [self.customNavigationBar setLeftItemWithIcon:[UIImage imageNamed:@"nav_back_logo"]];
    [self.customNavigationBar.leftButton addTarget:self action:@selector(__backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)__backButtonClicked:(UIButton *)sender{
    if(self.testType == 1){
        BOOL isPlay = NO;
        if(self.avPlayer&&self.isPlaying == YES){
            [self.avPlayer pause];
            self.isPlaying = NO;
            isPlay = YES;
        }
        //考试退出提示
        if(!self.quitAlertView){
            self.quitAlertView = [[TestQuitAlertView alloc]initWithFrame:CGRectMake(0, 0, self.currentAvailableWidth, self.currentAvailableHeight)];
        }
        UIWindow *window = KEY_WINDOW;
        if(window){
            [window addSubview:self.quitAlertView];
        }
        __weak typeof(self)self_weak_ = self;
        [self.quitAlertView setQuitBlock:^(NSInteger index) {
            [self_weak_.quitAlertView removeFromSuperview];
            if(index == 1){
                if(self_weak_.isFinish == YES){
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshPaperTypeData" object:nil];
                }
                if(self_weak_.avPlayer){
                    [self_weak_.avPlayer pause];
                    self_weak_.avPlayer = nil;
                }
                [self_weak_ remove];
                [self_weak_.navigationController popViewControllerAnimated:YES];
            }else{
                if(self_weak_.avPlayer&&self_weak_.isPlaying == NO&&isPlay == YES){
                    if (CMTimeCompare(self_weak_.avPlayer.currentItem.currentTime, self_weak_.avPlayer.currentItem.duration) == 0) {
                        [self_weak_.avPlayer seekToTime:kCMTimeZero];
                    }
                    [self_weak_.avPlayer play];
                    self_weak_.isPlaying = YES;
                }
            }
        }];
    }else{
        if(self.avPlayer){
            [self.avPlayer pause];
            self.avPlayer = nil;
        }
        if(self.isFinish == YES){
            [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshPaperTypeData" object:nil];
        }
        [self remove];
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark - 倒计时
-(void)updateTime{
    
    _seconds = _seconds+60.0;
    if(_seconds < _totalSeconds){
        NSInteger hour = (_totalSeconds - _seconds)/3600;
        NSInteger min = (_totalSeconds - _seconds - hour*3600)/60;
        [self.countDownView refreshWithHour:[NSString stringWithFormat:@"%02ld",(long)hour] andMinute:[NSString stringWithFormat:@"%02ld",(long)min]];
    }else{
        //计时结束
        if(self.timer){
            [self.timer invalidate];
            self.timer = nil;
        }
        if([[self currentViewController] isKindOfClass:[ExamQuestionsViewController class]]){
            if(self.isHaveListen == YES){
                //有听力进入听力考试
                [self showListenAlertView];
            }else{
                //没有就直接交卷
                [self commitWithPtId:@""];
            }
        }
    }
    
}


#pragma mark - 只看错题
-(void)onlyErrorBtnClick{
    self.onlyErrorBtn.selected = !self.onlyErrorBtn.selected;
    [self.answerSheetArr removeAllObjects];
    self.loadingHub = [JPProgressHUD showLoadingInView:self.view];
    if(self.onlyErrorBtn.selected == YES){
        [self loadQuestionResultDataWithCorrect:@"0"];
    }else{
        [self loadQuestionResultDataWithCorrect:@""];
    }
}

#pragma mark - 收藏 答题卡 错误反馈点击
-(void)btnClick:(UIButton*)button{
    NSInteger index = button.tag-666;
    if(index == 0){
        //收藏
        if(self.selQuestionModel){
            [self collectOrDeleteQuestionWithModel:self.selQuestionModel];
        }
    }else if (index == 1){
        //答题卡
        if(self.answerSheetArr.count > 0){
            //有数据直接显示
            [self showAnswerSheet];
        }else{
            //没数据获取数据
            [self loadAnswerSheetDataWithIsFirst:NO];
        }
    }else if (index == 2){
        //错误反馈
        BOOL isPlay = NO;
        if(self.avPlayer&&self.isPlaying == YES){
            [self.avPlayer pause];
            self.isPlaying = NO;
            isPlay = YES;
        }
        ErrorCorrectionViewController *vc = [[ErrorCorrectionViewController alloc]init];
        vc.type = 0;
        vc.paperModel = self.paperModel;
        vc.questionModel = self.selQuestionModel;
        [self.navigationController pushViewController:vc animated:YES];
        __weak typeof(self)weakSelf = self;
        [vc setBlackBlock:^{
              if(weakSelf.avPlayer&&weakSelf.isPlaying == NO&&isPlay == YES){
                  if (CMTimeCompare(weakSelf.avPlayer.currentItem.currentTime, weakSelf.avPlayer.currentItem.duration) == 0) {
                     [weakSelf.avPlayer seekToTime:kCMTimeZero];
                  }
                  [weakSelf.avPlayer play];
                  weakSelf.isPlaying = YES;
              }
        }];
    }
}

#pragma mark - 获取答题卡数据
-(void)loadAnswerSheetDataWithIsFirst:(BOOL)isFirst{
    //没数据获取数据
    if(self.testType == 2){
        //考试结果
        self.resultAnswerSheetManager.isFirst = isFirst;
        self.resultAnswerSheetManager.paperId = self.paperModel.paperId;
        NSString *correct = (self.onlyErrorBtn.selected == YES)?@"0":@"";
        self.resultAnswerSheetManager.correct = correct;
        self.resultAnswerSheetManager.ptId = @"";
        [self.resultAnswerSheetManager loadData];
        
    }else if (self.testType == 3){
        //专项结果
        self.resultAnswerSheetManager.isFirst = isFirst;
        self.resultAnswerSheetManager.paperId = self.paperModel.paperId;
        NSString *correct = (self.onlyErrorBtn.selected == YES)?@"0":@"";
        self.resultAnswerSheetManager.correct = correct;
        self.resultAnswerSheetManager.ptId = self.typeModel.ptId;
        [self.resultAnswerSheetManager loadData];
    }else{
        //专项测试或者试卷
        self.answerSheetManager.isFirst = isFirst;
        self.answerSheetManager.paperId = self.paperModel.paperId;
        if(self.testType == 0){
            self.answerSheetManager.ptId = self.typeModel.ptId;
            self.answerSheetManager.isRestart = @"0";
        }else{
            self.answerSheetManager.ptId = @"";
            self.answerSheetManager.isRestart = @"1";
        }
        [self.answerSheetManager loadData];
    }
}

#pragma mark - 试题收藏和取消收藏
-(void)collectOrDeleteQuestionWithModel:(ExamQuestionModel*)model{
    if(self.collectBtn.selected == YES){
        //取消
        self.collectDeleteManager.collectId = model.collectId;
        [self.collectDeleteManager loadData];
    }else{
        //收藏
        self.collectSaveManager.wordId = model.questionId;
        self.collectSaveManager.type = @"2";
        [self.collectSaveManager loadData];
    }
}


#pragma mark - 答题卡
-(void)showAnswerSheet{
    if(!self.answerSheetView){
        self.answerSheetView = [[ExamAnswerSheetView alloc]initWithFrame:CGRectMake(0, 0, self.currentAvailableWidth, self.currentAvailableHeight)];
    }
    self.answerSheetView.currentAvailableWidth = self.currentAvailableWidth;
    self.answerSheetView.currentAvailableHeight = self.currentAvailableHeight;
    UIWindow *keyWindow = KEY_WINDOW;
    if(keyWindow){
        self.answerSheetView.testType = self.testType;
        self.answerSheetView.count = self.answerSheetCount?self.answerSheetCount:@"0";
        self.answerSheetView.answerArr = self.answerArr;
        self.answerSheetView.answerSheetArr = self.answerSheetArr;
        [keyWindow addSubview:self.answerSheetView];
        [self.answerSheetView show];
        __weak typeof(self)self_weak_ = self;
        [self.answerSheetView setClickBlock:^(ExamPaperAnswerSheetRecordModel * _Nonnull recordModel) {
            [self_weak_.answerSheetView hidden];
            [self_weak_ scrollWithRecordModel:recordModel];
        }];
    }
}

#pragma mark - 点击了答题卡定位到某一个题目
-(void)scrollWithRecordModel:(ExamPaperAnswerSheetRecordModel*)recordModel{
    
        for(NSInteger i = 0;i < self.dataArr.count;i++){
            ExamQuestionModel *model = self.dataArr[i];
            if(model.isParallel&&[model.isParallel isEqualToString:@"1"]){
                if([model.bigQuestionId isEqualToString:recordModel.bigQuestionId]){
                    [self.collectionView setContentOffset:CGPointMake(i*self.currentAvailableWidth,0)];
                    BOOL ishave = NO;
                    for(NSInteger j = 0;j < model.questionList.count;j++){
                        ExamQuestionModel *model2 = model.questionList[j];
                        if([model2.sortId isEqualToString:recordModel.sort]){
                            //处理阅读题的选中
                            model.read_question_index = j;
                            NSMutableArray *qarr = [NSMutableArray arrayWithArray:model.questionList];
                            [qarr replaceObjectAtIndex:j withObject:model2];
                            model.questionList = qarr;
                            [self.dataArr replaceObjectAtIndex:i withObject:model];
                            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
                            [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
                            ishave = YES;
                            break;
                        }
                    }
                    if(ishave == YES){
                        break;
                    }
                }
            }else{
                if([model.sortId isEqualToString:recordModel.sort]&&[model.bigQuestionId isEqualToString:recordModel.bigQuestionId]){
                    [self.collectionView setContentOffset:CGPointMake(i*self.currentAvailableWidth,0)];
                    break;
                }
            }
        }

}


#pragma mark collectionView代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.item < self.dataArr.count){
        ExamQuestionModel *model = self.dataArr[indexPath.item];
        if([model.type isEqualToString:@"36"]){
            //写作
            ExamQuestionWriteCollectionViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"ExamQuestionWriteCollectionViewCell" forIndexPath:indexPath];
            cell.currentAvailableWidth = self.currentAvailableWidth;
            cell.row = indexPath.item;
            cell.testType = self.testType;
            cell.model = model;
            return cell;
            
        }
        
        if((model.isParallel&&[model.isParallel isEqualToString:@"1"])){
            //阅读 小问题并排显示
            ExamQuestionReadCollectionViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"ExamQuestionReadCollectionViewCell" forIndexPath:indexPath];
            cell.delegate = self;
            cell.currentAvailableWidth = self.currentAvailableWidth;
            cell.currentAvailableHeight = self.currentAvailableHeight;
            cell.item = indexPath.item;
            cell.testType = self.testType;
            cell.model = model;
            [cell createQuestionUI];
            return cell;
        }
        
        ExamQuestionsCollectionViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"ExamQuestionsCollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.currentAvailableWidth = self.currentAvailableWidth;
        cell.item = indexPath.item;
        cell.testType = self.testType;
        cell.model = model;
        [cell refeshUI];
        return cell;
    }
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.currentAvailableWidth,self.currentAvailableHeight-kYM_NAVIGATIONBAR_HEIGHT-38);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
     return UIEdgeInsetsMake(0,0,0,0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
     return 0;
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
     return 0;
}

#pragma mark - scrollview delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if([scrollView isEqual:self.collectionView]){
        NSInteger index = scrollView.contentOffset.x/self.currentAvailableWidth;
        [self scrollRefreshWithIndex:index];
    }
}

-(void)scrollRefreshWithIndex:(NSInteger)index{
    self.big_question_index = index;
    if(index < self.dataArr.count){
        ExamQuestionModel *model = self.dataArr[index];
        if(model.isParallel&&[model.isParallel isEqualToString:@"1"]){
            if(model.read_question_index < model.questionList.count){
                ExamQuestionModel *model2 = model.questionList[model.read_question_index];
                if(self.testType == 2||self.testType == 3||(self.testType == 0&&self.typeModel.typeId&&self.typeModel.typeId.integerValue == 55)){
                    self.countLabel.text = [NSString stringWithFormat:@"%ld/%ld",model2.scroll_index,self.totalCount];
                }
                self.selQuestionModel = model2;
                if([model2.collect isEqualToString:@"1"]){
                    self.collectBtn.selected = YES;
                }else{
                    self.collectBtn.selected = NO;
                }
                if(self.testType == 2){
                    //考试结果的时候 只有听力专项才显示听力按钮
                    if(model2.type&&model2.type.integerValue == 33){
                        self.listenImageView.hidden = NO;
                    }else{
                        self.listenImageView.hidden = YES;
                    }
                }
                if(self.testType == 0||self.testType == 3){
                    if([self.typeModel.typeId isEqualToString:@"33"]){
                        //听力专项 暂停播放听力
                        if(self.avPlayer){
                            self.isPlaying = NO;
                            [self.avPlayer pause];
                            [self stopVoiceAnimation];
                            [self.avPlayer seekToTime:kCMTimeZero];
                        }
                    }
                }
            }
        }else{
            if(self.testType == 2||self.testType == 3||(self.testType == 0&&self.typeModel.typeId&&self.typeModel.typeId.integerValue == 55)){
                self.countLabel.text = [NSString stringWithFormat:@"%ld/%ld",model.scroll_index,self.totalCount];
            }
            self.selQuestionModel = model;
            if([model.collect isEqualToString:@"1"]){
                self.collectBtn.selected = YES;
            }else{
                self.collectBtn.selected = NO;
            }
            if(self.testType == 2){
                //考试结果的时候 只有听力专项才显示听力按钮
                if(model.type&&model.type.integerValue == 33){
                    self.listenImageView.hidden = NO;
                }else{
                    self.listenImageView.hidden = YES;
                }
            }
            if(self.testType == 0||self.testType == 3){
                if([self.typeModel.typeId isEqualToString:@"33"]){
                    //听力专项 播放听力
                    if(self.avPlayer){
                        self.isPlaying = NO;
                        [self.avPlayer pause];
                        [self stopVoiceAnimation];
                        [self.avPlayer seekToTime:kCMTimeZero];
                    }
                }
            }
        }
        
    }
}

#pragma mark - 听力点击
-(void)listenPlayTapGesture{
    if(self.testType == 0||self.testType == 2||self.testType == 3){
        //只有专项和查看答题时可以点击
        if([self.typeModel.typeId isEqualToString:@"33"]){
            NSString *hearingFile = self.selQuestionModel.hearingFile?self.selQuestionModel.hearingFile:@"";
            if(self.big_question_index < self.dataArr.count){
                ExamQuestionModel *model = self.dataArr[self.big_question_index];
                if(model.isParallel&&[model.isParallel isEqualToString:@"1"]){
                    if(model.isSideNumber == YES){
                        //如果是特殊并排小题 取小题的第一个听力文件
                        if(model.questionList.count > 0){
                            ExamQuestionModel *qmodel = model.questionList[0];
                            hearingFile = qmodel.hearingFile;
                        }
                    }else{
                        hearingFile = model.hearingFile;
                    }
                }
            }
            //专项点击只播放当前题的
            if([self.currentHearingFile isEqualToString:hearingFile]){
                //还是当前的题目
                if(self.avPlayer){
                    if (self.isPlaying == YES){
                        [self.avPlayer pause];
                        [self stopVoiceAnimation];
                    } else {
                        if (CMTimeCompare(self.avPlayer.currentItem.currentTime, self.avPlayer.currentItem.duration) == 0) {
                            [self.avPlayer seekToTime:kCMTimeZero];
                        }
                        [self.avPlayer play];
                        [self startVoiceAnimation];
                    }
                    self.isPlaying = !self.isPlaying;
                }else{
                    [self playListenWithHearingFile:hearingFile];
                }
            }else{
                //重新播放新的
                [self playListenWithHearingFile:hearingFile];
            }
            
        }else{
            if(self.avPlayer){
                if (self.isPlaying == YES){
                    [self.avPlayer pause];
                    [self stopVoiceAnimation];
                } else {
                    [self.avPlayer play];
                    [self startVoiceAnimation];
                }
                self.isPlaying = !self.isPlaying;
            }else{
                [self playListenWithHearingFile:self.paperModel.hearingFile];
            }
        }
    }
}

#pragma mark - 听力播放
-(void)playListenWithHearingFile:(NSString*)hearingFile{
    self.listenImageView.hidden = NO;
    if(hearingFile&&![hearingFile isEqualToString:@""]){
        self.currentHearingFile = hearingFile;
        AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:hearingFile]];
        [item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
        if(self.avPlayer){
            [self.avPlayer pause];
            [self.avPlayer replaceCurrentItemWithPlayerItem:item];
        }else{
            self.avPlayer = [[AVPlayer alloc] initWithPlayerItem:item];
        }
        self.currentItem = item;
        [self.avPlayer play];
        self.isPlaying = YES;
    }else{
        [JPProgressHUD showMsg:@"听力文件不存在"];
    }
}


-(void)playToEndTime:(NSNotification*)notifi{
    [self stopVoiceAnimation];
    [self.avPlayer seekToTime:kCMTimeZero];
    self.isPlaying = NO;
}


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"status"]) {
        AVPlayerItemStatus status = [change[NSKeyValueChangeNewKey] integerValue];
        if (status == AVPlayerItemStatusReadyToPlay) {
            NSLog(@"播放成功");
            [self startVoiceAnimation];
        } else if (status == AVPlayerItemStatusFailed) {
            NSLog(@"播放失败");
            [JPProgressHUD showMsg:@"听力播放失败"];
            [self stopVoiceAnimation];
        }
    }
}


#pragma mark - 开始播放动画
-(void)startVoiceAnimation{
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"listen_play.gif" ofType:nil];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    self.listenImageView.image = [UIImage sd_imageWithGIFData:imageData];
}

#pragma mark - 结束播放动画
-(void)stopVoiceAnimation{
    self.listenImageView.image = [UIImage imageNamed:@"test_voice_play"];
}


#pragma mark - ExamQuestionsCollectionViewCellDelegate
-(void)answerQuestionTap:(ExamQuestionsCollectionViewCell *)cell andOptionsModel:(ExamQuestionOptionsModel *)optionsModel{
    //选择了答案
    self.selCell = cell;
    self.sel_index = cell.item;
    [self answerQustionWithQuestionId:cell.model.questionId andAnswer:optionsModel.optionKey andIsDone:@"0"];
}

-(void)checkQuestionTap:(ExamQuestionsCollectionViewCell *)cell{
    //查看答案
    if(self.testType  == 0){
        self.selCell = cell;
        self.sel_index = cell.item;
        if([cell.model.isSelect isEqualToString:@"1"]||cell.model.isSelect.integerValue == 1){
            [self answerQustionWithQuestionId:cell.model.questionId andAnswer:cell.model.doAnswer andIsDone:@"1"];
        }else{
            //直接刷新
            ExamQuestionModel *model = cell.model;
            model.isDone = @"1";
            [self.dataArr replaceObjectAtIndex:self.sel_index withObject:model];
            cell.model = model;
            [cell refreshAnalysisUIWithModel:model];
        }
    }
}

#pragma mark - ExamQuestionReadCollectionViewCellDelegate
-(void)answerReadQuestionTap:(ExamQuestionReadCollectionViewCell *)cell andQuestionModel:(nonnull ExamQuestionModel *)model andOptionsModel:(nonnull ExamQuestionOptionsModel *)optionsModel{
    //选择了答案
    self.readCell = cell;
    self.sel_index = cell.item;
    [self answerQustionWithQuestionId:model.questionId andAnswer:optionsModel.optionKey andIsDone:@"0"];
}

-(void)checkReadQuestionTap:(ExamQuestionReadCollectionViewCell *)cell andQuestionModel:(ExamQuestionModel *)model{
    //查看答案
    if(self.testType  == 0){
        self.readCell = cell;
        self.sel_index = cell.item;
        if([model.isSelect isEqualToString:@"1"]||model.isSelect.integerValue == 1){
            [self answerQustionWithQuestionId:model.questionId andAnswer:model.doAnswer andIsDone:@"1"];
        }else{
            //直接替换数据刷新
            ExamQuestionModel *bigModel = cell.model;
            NSMutableArray *qarr = [NSMutableArray arrayWithArray:bigModel.questionList];
            if(bigModel.read_question_index < qarr.count){
                ExamQuestionModel *qmodel = qarr[bigModel.read_question_index];
                qmodel.isDone = @"1";
                [qarr replaceObjectAtIndex:bigModel.read_question_index withObject:qmodel];
            }
            bigModel.questionList = qarr;
            [self.dataArr replaceObjectAtIndex:self.sel_index withObject:bigModel];
            cell.model = bigModel;
            [cell refreshCheck];
        }
    }
}

-(void)readQuestionScroll:(ExamQuestionReadCollectionViewCell *)cell andQuestionModel:(ExamQuestionModel *)model andIsNext:(NSInteger)isNext{
    if(self.big_question_index == cell.item){
        if(isNext == 1){
            if(cell.item+1 < self.dataArr.count){
                self.big_question_index = cell.item+1;
                [self.collectionView setContentOffset:CGPointMake((cell.item+1)*self.currentAvailableWidth,0) animated:YES];
            }
        }else if(isNext == 2){
            if((cell.item-1 >= 0)&&(cell.item-1 < self.dataArr.count)){
                self.big_question_index = cell.item-1;
                [self.collectionView setContentOffset:CGPointMake((cell.item-1)*self.currentAvailableWidth,0) animated:YES];
            }
        }else{
            if(self.testType == 2||self.testType == 3||(self.testType == 0&&self.typeModel.typeId&&self.typeModel.typeId.integerValue == 55)){
                self.countLabel.text = [NSString stringWithFormat:@"%ld/%ld",model.scroll_index,self.totalCount];
            }
            self.selQuestionModel = model;
            if([model.collect isEqualToString:@"1"]){
                self.collectBtn.selected = YES;
            }else{
                self.collectBtn.selected = NO;
            }
            if(cell.item < self.dataArr.count){
                [self.dataArr replaceObjectAtIndex:cell.item withObject:cell.model];
            }
        }
    }
}


#pragma mark - JPAPIManagerCallBackDelegate
-(void)managerCallDLDLAPIDidSuccess:(JPAPIBaseManager *)manager{
    
    if([manager isEqual:self.questionManager]){
        NSDictionary *dic = manager.response.content;
        NSArray *arr = [ExamQuestionModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        self.totalCount = 0;
        NSInteger scroll_index = 0;
        NSInteger l_scroll_index = 0;
        NSInteger finish_index = 0;//专项测试选择到第几题
        self.listenCount = 0;
        for(NSInteger i = 0;i < arr.count;i++){
            ExamQuestionModel *model = arr[i];
            if((model.type.integerValue == 35||model.type.integerValue == 56||model.type.integerValue == 55)&&model.paperReadList){
                //阅读题处理
                model.isParallel = @"1";
                NSArray *paperReadList = [ExamQuestionReadListModel mj_objectArrayWithKeyValuesArray:model.paperReadList];
                for(NSInteger j = 0;j < paperReadList.count;j++){
                    ExamQuestionModel *questionModel = [[ExamQuestionModel alloc]init];
                    questionModel.bigQuestionId = model.bigQuestionId;
                    questionModel.bigQuestionContent = model.bigQuestionContent;
                    questionModel.type = model.type;
                    questionModel.paperId = model.paperId;
                    questionModel.read_question_index = 0;
                    questionModel.isParallel = model.isParallel;
                    questionModel.bsort = model.bsort;
                    questionModel.name = model.name;
                    ExamQuestionReadListModel *readmodel = paperReadList[j];
                    questionModel.readContent = readmodel.content;
                    questionModel.read_list_index = j;
                    NSArray *questionList = [ExamQuestionModel mj_objectArrayWithKeyValuesArray:readmodel.examQuestions];
                    NSMutableArray *qarr = [NSMutableArray array];
                    for(NSInteger k = 0;k < questionList.count;k++){
                        ExamQuestionModel *qmodel = questionList[k];
                        qmodel.bsort = model.bsort;
                        qmodel.isParallel = model.isParallel;
                        qmodel.name = model.name;
                        NSArray *questionOptions2 = [ExamQuestionOptionsModel mj_objectArrayWithKeyValuesArray:qmodel.questionOptions];
                        qmodel.questionOptions = questionOptions2;
                        scroll_index++;
                        qmodel.scroll_index = scroll_index;
                        if(self.testType == 2||self.testType == 3||(self.testType == 0&&self.typeModel.typeId&&self.typeModel.typeId.integerValue == 55)){
                            self.totalCount = self.totalCount+1;
                        }else{
                            if(!(self.testType == 1&&[qmodel.type isEqualToString:@"33"])){
                                if([qmodel.isSelect isEqualToString:@"1"]||qmodel.isSelect.integerValue == 1){
                                    //只有选择题才算在答题总数里
                                    self.totalCount = self.totalCount+1;
                                }
                            }else{
                                if([qmodel.isSelect isEqualToString:@"1"]||qmodel.isSelect.integerValue == 1){
                                    self.listenCount = self.listenCount+1;
                                }
                            }
                        }
                        [qarr addObject:qmodel];
                        if(self.testType == 0){
                            //把回答过的加进数组
                            if(qmodel.isDone&&([qmodel.isSelect isEqualToString:@"1"]||qmodel.isSelect.integerValue == 1)){
                                finish_index = self.dataArr.count+1;
                                if(![self.answerArr containsObject:qmodel.questionId]){
                                    [self.answerArr addObject:qmodel.questionId];
                                }
                            }
                        }
                    }
                    questionModel.questionList = qarr;
                    [self.dataArr addObject:questionModel];
                  
                }
                
            }else{
                
                //除写作题外 其他题处理
                if(![model.type isEqualToString:@"36"]){
                    NSArray *questionList = [ExamQuestionModel mj_objectArrayWithKeyValuesArray:model.questionList];
                    if(model.sideNumber&&![model.sideNumber isEqualToString:@""]){
                        //部分小题并排显示
                        NSMutableArray *sideArr = [NSMutableArray array];//抽出来每个组的问题数组
                        NSMutableArray *sortArr = [NSMutableArray array];//有并排的小题序号数组
                        NSMutableArray *indexArr = [NSMutableArray array];//每组的第一个序号下标
                        //每一个并排的小组
                        NSArray *sqarr = [model.sideNumber componentsSeparatedByString:@"&"];
                        for(NSInteger s = 0;s < sqarr.count;s++){
                            NSString *sideStr = [NSString stringWithFormat:@"%@",sqarr[s]];
                            NSArray *arr2 = [sideStr componentsSeparatedByString:@","];
                            NSMutableArray *arr3 = [NSMutableArray array];
                            for(NSInteger j = 0;j < arr2.count;j++){
                                NSString *lsort = [NSString stringWithFormat:@"%@",arr2[j]];
                                [sortArr addObject:lsort];
                                for(NSInteger k = 0;k < questionList.count;k++){
                                    ExamQuestionModel *qmodel = questionList[k];
                                    NSArray *questionOptions2 = [ExamQuestionOptionsModel mj_objectArrayWithKeyValuesArray:qmodel.questionOptions];
                                    qmodel.questionOptions = questionOptions2;
                                    qmodel.bsort = model.bsort;
                                    if(qmodel.lsort&&[qmodel.lsort isEqualToString:lsort]){
                                        [arr3 addObject:qmodel];
                                        if(j == 0&&![indexArr containsObject:[NSString stringWithFormat:@"%ld",k]]){
                                            [indexArr addObject:[NSString stringWithFormat:@"%ld",k]];
                                        }
                                    }
                                }
                            }
                            [sideArr addObject:arr3];
                        }
                        //看sortArr有没有数据 有数据就并排显示 没数据就单独显示
                        for(NSInteger k = 0;k < questionList.count;k++){
                            ExamQuestionModel *questionModel = [[ExamQuestionModel alloc]init];
                            ExamQuestionModel *qmodel = questionList[k];
                            [self changeModel:questionModel withQmodel:qmodel];
                            questionModel.bigQuestionId = model.bigQuestionId;
                            questionModel.bigQuestionContent = model.bigQuestionContent;
                            questionModel.isParallel = model.isParallel;
                            questionModel.bsort = model.bsort;
                            questionModel.name = model.name;
                            if(self.testType == 2||self.testType == 3||(self.testType == 0&&self.typeModel.typeId&&self.typeModel.typeId.integerValue == 55)){
                                self.totalCount = self.totalCount+1;
                            }else{
                                if(!(self.testType == 1&&[qmodel.type isEqualToString:@"33"])){
                                    if([questionModel.isSelect isEqualToString:@"1"]||questionModel.isSelect.integerValue == 1){
                                        //只有选择题才算在答题总数里
                                        self.totalCount = self.totalCount+1;
                                    }
                                }else{
                                    if([questionModel.isSelect isEqualToString:@"1"]||questionModel.isSelect.integerValue == 1){
                                        //只有选择题才算在答题总数里
                                        self.listenCount = self.listenCount+1;
                                    }
                                }
                            }
                            if(self.testType == 0){
                                //把回答过的加进数组
                                if(questionModel.isDone&&([questionModel.isSelect isEqualToString:@"1"]||questionModel.isSelect.integerValue == 1)){
                                    finish_index = self.dataArr.count+1;
                                    if(![self.answerArr containsObject:questionModel.questionId]){
                                        [self.answerArr addObject:questionModel.questionId];
                                    }
                                }
                            }
                            if([sortArr containsObject:qmodel.lsort]){
                                //并排加
                                if([indexArr containsObject:[NSString stringWithFormat:@"%ld",k]]){
                                    //是并排的第一个的才加
                                    questionModel.isParallel = @"1";
                                    questionModel.isSideNumber = YES;
                                    //拿出是第几个问题数组
                                    NSInteger qIndex = 0;
                                    for(NSInteger l = 0;l < indexArr.count;l++){
                                        NSString *index = indexArr[l];
                                        if(index.integerValue == k){
                                            qIndex = l;
                                            break;
                                        }
                                    }
                                    if(qIndex < sideArr.count){
                                        NSArray *list = sideArr[qIndex];
                                        //滚动下标赋值
                                        NSMutableArray *qqarr = [NSMutableArray arrayWithArray:list];
                                        for(NSInteger m = 0;m < qqarr.count;m++){
                                            ExamQuestionModel *qqmodel = qqarr[m];
                                            if([questionModel.type isEqualToString:@"33"]){
                                                if(self.testType == 1){
                                                    //听力题另外保存
                                                    l_scroll_index++;
                                                    qqmodel.scroll_index = l_scroll_index;
                                                }else{
                                                    scroll_index++;
                                                    qqmodel.scroll_index = scroll_index;
                                                }
                                            }else{
                                                scroll_index++;
                                                qqmodel.scroll_index = scroll_index;
                                            }
                                            [qqarr replaceObjectAtIndex:m withObject:qqmodel];
                                            if(m == 0){
                                                questionModel.mergeContent = qmodel.content;
                                            }
                                        }
                                        questionModel.questionList = qqarr;
                                        if([questionModel.type isEqualToString:@"33"]){
                                            if(self.testType == 1){
                                                //听力题另外保存
                                                [self.listenArr addObject:questionModel];
                                            }else{
                                                [self.dataArr addObject:questionModel];
                                            }
                                        }else{
                                            [self.dataArr addObject:questionModel];
                                        }
                                    }
                                }
                            }else{
                                //单独加
                                questionModel.isParallel = @"0";
                                questionModel.isSideNumber = NO;
                                NSArray *questionOptions2 = [ExamQuestionOptionsModel mj_objectArrayWithKeyValuesArray:qmodel.questionOptions];
                                questionModel.questionOptions = questionOptions2;
                                if([questionModel.type isEqualToString:@"33"]){
                                    if(self.testType == 1){
                                        //听力题另外保存
                                        l_scroll_index++;
                                        questionModel.scroll_index = l_scroll_index;
                                        [self.listenArr addObject:questionModel];
                                    }else{
                                        scroll_index++;
                                        questionModel.scroll_index = scroll_index;
                                        [self.dataArr addObject:questionModel];
                                    }
                                }else{
                                    scroll_index++;
                                    questionModel.scroll_index = scroll_index;
                                    [self.dataArr addObject:questionModel];
                                }
                            }
                        }
                        
                    }else{
                        
                        
                        if(model.isParallel&&[model.isParallel isEqualToString:@"1"]){
                            //并排显示小题
                            NSMutableArray *qarr = [NSMutableArray array];
                            for(NSInteger k = 0;k < questionList.count;k++){
                                ExamQuestionModel *qmodel = questionList[k];
                                qmodel.bsort = model.bsort;
                                qmodel.isParallel = model.isParallel;
                                qmodel.name = model.name;
                                NSArray *questionOptions2 = [ExamQuestionOptionsModel mj_objectArrayWithKeyValuesArray:qmodel.questionOptions];
                                qmodel.questionOptions = questionOptions2;
                                scroll_index++;
                                qmodel.scroll_index = scroll_index;
                                if(self.testType == 2||self.testType == 3||(self.testType == 0&&self.typeModel.typeId&&self.typeModel.typeId.integerValue == 55)){
                                    self.totalCount = self.totalCount+1;
                                }else{
                                    if(!(self.testType == 1&&[qmodel.type isEqualToString:@"33"])){
                                        if([qmodel.isSelect isEqualToString:@"1"]||qmodel.isSelect.integerValue == 1){
                                            //只有选择题才算在答题总数里
                                            self.totalCount = self.totalCount+1;
                                        }
                                    }else{
                                        if([qmodel.isSelect isEqualToString:@"1"]||qmodel.isSelect.integerValue == 1){
                                            //只有选择题才算在答题总数里
                                            self.listenCount = self.listenCount+1;
                                        }
                                    }
                                }
                                [qarr addObject:qmodel];
                                if(self.testType == 0){
                                    //把回答过的加进数组
                                    if(qmodel.isDone&&([qmodel.isSelect isEqualToString:@"1"]||qmodel.isSelect.integerValue == 1)){
                                        finish_index = self.dataArr.count+1;
                                        if(![self.answerArr containsObject:qmodel.questionId]){
                                            [self.answerArr addObject:qmodel.questionId];
                                        }
                                    }
                                }
                            }
                            model.questionList = qarr;
                            model.read_question_index = 0;
                            if([model.type isEqualToString:@"33"]){
                                if(self.testType == 1){
                                    //听力题另外保存
                                    l_scroll_index++;
                                    model.scroll_index = l_scroll_index;
                                    [self.listenArr addObject:model];
                                }else{
                                    scroll_index++;
                                    model.scroll_index = scroll_index;
                                    [self.dataArr addObject:model];
                                }
                            }else{
                                scroll_index++;
                                model.scroll_index = scroll_index;
                                [self.dataArr addObject:model];
                            }
                        }else{
                            //单独显示小题
                            for(NSInteger k = 0;k < questionList.count;k++){
                                ExamQuestionModel *questionModel = [[ExamQuestionModel alloc]init];
                                ExamQuestionModel *qmodel = questionList[k];
                                [self changeModel:questionModel withQmodel:qmodel];
                                questionModel.bigQuestionId = model.bigQuestionId;
                                questionModel.bigQuestionContent = model.bigQuestionContent;
                                questionModel.isParallel = model.isParallel;
                                questionModel.bsort = model.bsort;
                                questionModel.name = model.name;
                                NSArray *questionOptions2 = [ExamQuestionOptionsModel mj_objectArrayWithKeyValuesArray:qmodel.questionOptions];
                                questionModel.questionOptions = questionOptions2;
                                if(self.testType == 2||self.testType == 3||(self.testType == 0&&self.typeModel.typeId&&self.typeModel.typeId.integerValue == 55)){
                                    self.totalCount = self.totalCount+1;
                                }else{
                                    if(!(self.testType == 1&&[qmodel.type isEqualToString:@"33"])){
                                        if([questionModel.isSelect isEqualToString:@"1"]){
                                            //只有选择题才算在答题总数里
                                            self.totalCount = self.totalCount+1;
                                        }
                                    }else{
                                        if([questionModel.isSelect isEqualToString:@"1"]){
                                            //只有选择题才算在答题总数里
                                            self.listenCount = self.listenCount+1;
                                        }
                                    }
                                }
                                if(self.testType == 0){
                                    //把回答过的加进数组
                                    if(questionModel.isDone&&([questionModel.isSelect isEqualToString:@"1"]||questionModel.isSelect.integerValue == 1)){
                                        finish_index = self.dataArr.count+1;
                                        if(![self.answerArr containsObject:questionModel.questionId]){
                                            [self.answerArr addObject:questionModel.questionId];
                                        }
                                    }
                                }
                                if([questionModel.type isEqualToString:@"33"]){
                                    if(self.testType == 1){
                                        //听力题另外保存
                                        l_scroll_index++;
                                        questionModel.scroll_index = l_scroll_index;
                                        [self.listenArr addObject:questionModel];
                                    }else{
                                        scroll_index++;
                                        questionModel.scroll_index = scroll_index;
                                        [self.dataArr addObject:questionModel];
                                    }
                                }else{
                                    scroll_index++;
                                    questionModel.scroll_index = scroll_index;
                                    [self.dataArr addObject:questionModel];
                                }
                            }
                        }
                    }
                }else{
                    //写作题
                    scroll_index++;
                    model.scroll_index = scroll_index;
                    if(self.testType == 2||self.testType == 3||(self.testType == 0&&self.typeModel.typeId&&self.typeModel.typeId.integerValue == 55)){
                        self.totalCount = self.totalCount+1;
                    }
                    [self.dataArr addObject:model];
                }
                
            }
            
        }
              
        if((self.answerArr.count > 0) && (self.answerArr.count == self.totalCount)){
            [self.commitBtn setTitleColor:UIColorHex(0xFE4A4B) forState:UIControlStateNormal];
        }
        if(self.testType == 2||self.testType == 3||(self.testType == 0&&self.typeModel.typeId&&self.typeModel.typeId.integerValue == 55)){
            //查看结果时是滑动的下标数
            if(self.totalCount > 0){
                self.countLabel.text = [NSString stringWithFormat:@"1/%ld",self.totalCount];
            }else{
                self.countLabel.text = @"0/0";
            }
        }else{
            //做题的时候是完成数
            self.countLabel.text = [NSString stringWithFormat:@"%ld/%ld",self.answerArr.count,self.totalCount];
        }
        
        [self.collectionView reloadData];

        //滑动到上一次记录
        [self scrollToLastRecoredWithFinishIndex:finish_index];
        if(self.loadingHub){
            [self.loadingHub removeFromSuperview];
        }
        
    }
    
    
    if([manager isEqual:self.questionResultManager]){
        NSDictionary *dic = manager.response.content;
        NSArray *arr = [ExamQuestionModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        self.totalCount = 0;
        NSInteger scroll_index = 0;
        for(NSInteger i = 0;i < arr.count;i++){
            ExamQuestionModel *model = arr[i];
            if((model.type.integerValue == 35||model.type.integerValue == 56||model.type.integerValue == 55)&&model.paperReadList){
                //阅读题处理
                model.isParallel = @"1";
                NSArray *paperReadList = [ExamQuestionReadListModel mj_objectArrayWithKeyValuesArray:model.paperReadList];
                for(NSInteger j = 0;j < paperReadList.count;j++){
                    ExamQuestionModel *questionModel = [[ExamQuestionModel alloc]init];
                    questionModel.bigQuestionId = model.bigQuestionId;
                    questionModel.bigQuestionContent = model.bigQuestionContent;
                    questionModel.type = model.type;
                    questionModel.paperId = model.paperId;
                    questionModel.read_question_index = 0;
                    questionModel.bsort = model.bsort;
                    questionModel.isParallel = model.isParallel;
                    ExamQuestionReadListModel *readmodel = paperReadList[j];
                    questionModel.readContent = readmodel.content;
                    questionModel.read_list_index = j;
                    questionModel.name = model.name;
                    NSArray *questionList = [ExamQuestionModel mj_objectArrayWithKeyValuesArray:readmodel.examQuestions];
                    self.totalCount = self.totalCount+questionList.count;
                    NSMutableArray *qarr = [NSMutableArray array];
                    for(NSInteger k = 0;k < questionList.count;k++){
                        ExamQuestionModel *qmodel = questionList[k];
                        qmodel.bsort = model.bsort;
                        qmodel.isParallel = model.isParallel;
                        NSArray *questionOptions2 = [ExamQuestionOptionsModel mj_objectArrayWithKeyValuesArray:qmodel.questionOptions];
                        qmodel.questionOptions = questionOptions2;
                        scroll_index++;
                        qmodel.scroll_index = scroll_index;
                        qmodel.name = model.name;
                        [qarr addObject:qmodel];
                    }
                    questionModel.questionList = qarr;
                    [self.dataArr addObject:questionModel];
                }
                
            }else{
                //除写作题外 其他题处理
                if(![model.type isEqualToString:@"36"]){
                    NSArray *questionList = [ExamQuestionModel mj_objectArrayWithKeyValuesArray:model.questionList];
                    if(model.sideNumber&&![model.sideNumber isEqualToString:@""]){
                        //部分小题并排显示
                        NSMutableArray *sideArr = [NSMutableArray array];//抽出来每个组的问题数组
                        NSMutableArray *sortArr = [NSMutableArray array];//有并排的小题序号数组
                        NSMutableArray *indexArr = [NSMutableArray array];//每组的第一个序号下标
                        //每一个并排的小组
                        NSArray *sqarr = [model.sideNumber componentsSeparatedByString:@"&"];
                        for(NSInteger s = 0;s < sqarr.count;s++){
                            NSString *sideStr = [NSString stringWithFormat:@"%@",sqarr[s]];
                            NSArray *arr2 = [sideStr componentsSeparatedByString:@","];
                            //错题的时候把没有返回的题移除 就是抽出题目有返回的小题的合并lsort
                            NSMutableArray *narr2 = [NSMutableArray array];
                            if(self.onlyErrorBtn.selected == YES){
                                for(NSInteger j = 0;j < arr2.count;j++){
                                    NSString *lsort = [NSString stringWithFormat:@"%@",arr2[j]];
                                    BOOL ishave = NO;
                                    for(NSInteger k = 0;k < questionList.count;k++){
                                        ExamQuestionModel *qmodel = questionList[k];
                                        if(qmodel.lsort&&[qmodel.lsort isEqualToString:lsort]){
                                            ishave = YES;
                                            break;
                                        }
                                    }
                                    if(ishave == YES){
                                        [narr2 addObject:lsort];
                                    }
                                }
                            }else{
                                narr2 = [NSMutableArray arrayWithArray:arr2];
                            }
                            NSMutableArray *arr3 = [NSMutableArray array];
                            for(NSInteger j = 0;j < narr2.count;j++){
                                NSString *lsort = [NSString stringWithFormat:@"%@",narr2[j]];
                                [sortArr addObject:lsort];
                                for(NSInteger k = 0;k < questionList.count;k++){
                                    ExamQuestionModel *qmodel = questionList[k];
                                    NSArray *questionOptions2 = [ExamQuestionOptionsModel mj_objectArrayWithKeyValuesArray:qmodel.questionOptions];
                                    qmodel.questionOptions = questionOptions2;
                                    qmodel.bsort = model.bsort;
                                    if(qmodel.lsort&&[qmodel.lsort isEqualToString:lsort]){
                                        [arr3 addObject:qmodel];
                                        if(j == 0&&![indexArr containsObject:[NSString stringWithFormat:@"%ld",k]]){
                                            [indexArr addObject:[NSString stringWithFormat:@"%ld",k]];
                                        }
                                    }
                                }
                            }
                            [sideArr addObject:arr3];
                        }
                        for(NSInteger k = 0;k < questionList.count;k++){
                            ExamQuestionModel *questionModel = [[ExamQuestionModel alloc]init];
                            ExamQuestionModel *qmodel = questionList[k];
                            qmodel.bsort = model.bsort;
                            qmodel.isParallel = model.isParallel;
                            [self changeModel:questionModel withQmodel:qmodel];
                            questionModel.bigQuestionId = model.bigQuestionId;
                            questionModel.bigQuestionContent = model.bigQuestionContent;
                            questionModel.isParallel = model.isParallel;
                            questionModel.name = model.name;
                            if([sortArr containsObject:qmodel.lsort]){
                                //并排加
                                if([indexArr containsObject:[NSString stringWithFormat:@"%ld",k]]){
                                    //是并排的第一个的才加
                                    questionModel.isParallel = @"1";
                                    questionModel.isSideNumber = YES;
                                    //拿出是第几个问题数组
                                    NSInteger qIndex = 0;
                                    for(NSInteger l = 0;l < indexArr.count;l++){
                                        NSString *index = indexArr[l];
                                        if(index.integerValue == k){
                                            qIndex = l;
                                            break;
                                        }
                                    }
                                    if(qIndex < sideArr.count){
                                        NSArray *list = sideArr[qIndex];
                                        //滚动下标赋值
                                        NSMutableArray *qqarr = [NSMutableArray arrayWithArray:list];
                                        for(NSInteger m = 0;m < qqarr.count;m++){
                                            ExamQuestionModel *qqmodel = qqarr[m];
                                            scroll_index++;
                                            qqmodel.scroll_index = scroll_index;
                                            [qqarr replaceObjectAtIndex:m withObject:qqmodel];
                                            if(m == 0){
                                                questionModel.mergeContent = qmodel.content;
                                            }
                                        }
                                        questionModel.questionList = qqarr;
                                        [self.dataArr addObject:questionModel];
                                    }
                                }
                            }else{
                                scroll_index++;
                                questionModel.scroll_index = scroll_index;
                                questionModel.isParallel = @"0";
                                questionModel.isSideNumber = NO;
                                NSArray *questionOptions2 = [ExamQuestionOptionsModel mj_objectArrayWithKeyValuesArray:qmodel.questionOptions];
                                questionModel.questionOptions = questionOptions2;
                                [self.dataArr addObject:questionModel];

                            }
                        }
                        self.totalCount = self.totalCount+questionList.count;
                        
                        
                    }else{
                        
                        
                        if(model.isParallel&&[model.isParallel isEqualToString:@"1"]){
                            //并排显示小题
                            NSMutableArray *qarr = [NSMutableArray array];
                            for(NSInteger k = 0;k < questionList.count;k++){
                                ExamQuestionModel *qmodel = questionList[k];
                                qmodel.bsort = model.bsort;
                                qmodel.isParallel = model.isParallel;
                                NSArray *questionOptions2 = [ExamQuestionOptionsModel mj_objectArrayWithKeyValuesArray:qmodel.questionOptions];
                                qmodel.questionOptions = questionOptions2;
                                scroll_index++;
                                qmodel.scroll_index = scroll_index;
                                qmodel.name = model.name;
                                [qarr addObject:qmodel];
                            }
                            model.questionList = qarr;
                            model.read_question_index = 0;
                            self.totalCount = self.totalCount+questionList.count;
                            [self.dataArr addObject:model];
                            
                        }else{
                            //单独显示小题
                            for(NSInteger k = 0;k < questionList.count;k++){
                                ExamQuestionModel *questionModel = [[ExamQuestionModel alloc]init];
                                ExamQuestionModel *qmodel = questionList[k];
                                qmodel.bsort = model.bsort;
                                qmodel.isParallel = model.isParallel;
                                [self changeModel:questionModel withQmodel:qmodel];
                                questionModel.bigQuestionId = model.bigQuestionId;
                                questionModel.bigQuestionContent = model.bigQuestionContent;
                                questionModel.isParallel = model.isParallel;
                                questionModel.name = model.name;
                                NSArray *questionOptions2 = [ExamQuestionOptionsModel mj_objectArrayWithKeyValuesArray:qmodel.questionOptions];
                                questionModel.questionOptions = questionOptions2;
                                scroll_index++;
                                questionModel.scroll_index = scroll_index;
                                [self.dataArr addObject:questionModel];
                            }
                            self.totalCount = self.totalCount+questionList.count;
                            
                        }
                    }
                }else{
                    //写作题
                    self.totalCount = self.totalCount+1;
                    scroll_index++;
                    model.scroll_index = scroll_index;
                    [self.dataArr addObject:model];
                }
  
            }
            
        }
        self.commitBtn.hidden = YES;
        //查看结果时是滑动的下标数
        if(self.totalCount > 0){
            self.countLabel.text = [NSString stringWithFormat:@"1/%ld",self.totalCount];
        }else{
            self.countLabel.text = @"0/0";
        }

        if(self.dataArr.count > 0){
            ExamQuestionModel *model0 = self.dataArr[0];
            self.big_question_index = 0;
            if(model0.isParallel&&[model0.isParallel isEqualToString:@"1"]){
                if(model0.questionList.count > 0){
                    ExamQuestionModel *model2 = model0.questionList[0];
                    self.selQuestionModel = model2;
                    if([model2.collect isEqualToString:@"1"]){
                        self.collectBtn.selected = YES;
                    }else{
                        self.collectBtn.selected = NO;
                    }
                    if(self.testType == 2){
                        if(model2.type&&model2.type.integerValue == 33){
                            self.listenImageView.hidden = NO;
                        }else{
                            self.listenImageView.hidden = YES;
                        }
                    }
                }
            }else if([model0.type isEqualToString:@"36"]){
                if([model0.collect isEqualToString:@"1"]){
                    self.collectBtn.selected = YES;
                }else{
                    self.collectBtn.selected = NO;
                }
                if(self.testType == 2){
                    self.listenImageView.hidden = YES;
                }
            }else{
                self.selQuestionModel = model0;
                if([model0.collect isEqualToString:@"1"]){
                    self.collectBtn.selected = YES;
                }else{
                    self.collectBtn.selected = NO;
                }
                if(self.testType == 2){
                    if(model0.type&&model0.type.integerValue == 33){
                        self.listenImageView.hidden = NO;
                    }else{
                        self.listenImageView.hidden = YES;
                    }
                }
            }
        }
        
        [self.collectionView reloadData];
        [self.collectionView setContentOffset:CGPointMake(0,0) animated:NO];
        if(self.loadingHub){
            [self.loadingHub removeFromSuperview];
        }
        
    }
    
    
    if([manager isEqual:self.updateQuestionManager]){
       NSDictionary *dic = manager.response.content;
       NSString *correct = [NSString stringWithFormat:@"%@",dic[@"data"][@"correct"]];
       NSString *doAnswer = [NSString stringWithFormat:@"%@",dic[@"data"][@"answer"]];
       NSString *isDone = [NSString stringWithFormat:@"%@",dic[@"data"][@"isDone"]?dic[@"data"][@"isDone"]:self.updateQuestionManager.isDone];
       if(self.sel_index < self.dataArr.count){
           //更新题目数据
           ExamQuestionModel *model = self.dataArr[self.sel_index];
           NSString *questionId = model.questionId;
           if(model.isParallel&&[model.isParallel isEqualToString:@"1"]){
               //并排
               NSMutableArray *qarr = [NSMutableArray arrayWithArray:model.questionList];
               if(model.read_question_index < qarr.count){
                   ExamQuestionModel *qmodel = qarr[model.read_question_index];
                   qmodel.correct = correct;
                   qmodel.doAnswer = doAnswer;
                   qmodel.isDone = isDone;
                   questionId = qmodel.questionId;
                   [qarr replaceObjectAtIndex:model.read_question_index withObject:qmodel];
                   if(![self.answerArr containsObject:qmodel.questionId]){
                       [self.answerArr addObject:qmodel.questionId];
                   }
               }
               model.questionList = qarr;
               [self.dataArr replaceObjectAtIndex:self.sel_index withObject:model];
               if(self.readCell){
                   self.readCell.model = model;
                   [self.readCell refreshCheck];
               }else{
                   NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.sel_index inSection:0];
                   [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
               }
           }else{
               model.correct = correct;
               model.doAnswer = doAnswer;
               model.isDone = isDone;
               [self.dataArr replaceObjectAtIndex:self.sel_index withObject:model];
               if(self.selCell){
                   self.selCell.model = model;
                   [self.selCell refreshAnalysisUIWithModel:model];
               }else{
                   NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.sel_index inSection:0];
                   [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
               }
               
               if(![self.answerArr containsObject:model.questionId]){
                   [self.answerArr addObject:model.questionId];
               }
           }
           
           //更新答题卡数据
           for(NSInteger i = 0;i < self.answerSheetArr.count;i++){
               ExamPaperAnswerSheetModel *amodel = self.answerSheetArr[i];
               if([amodel.typeId isEqualToString:model.type]){
                   NSArray *answers = amodel.answers;
                   NSMutableArray *answerArr = [NSMutableArray arrayWithArray:answers];
                   for(NSInteger j = 0;j < answers.count;j++){
                       ExamPaperAnswerSheetListModel *listModel = answers[j];
                       NSArray *examRecords = listModel.examRecords;
                       NSMutableArray *earr = [NSMutableArray arrayWithArray:examRecords];
                       for(NSInteger k = 0;k < examRecords.count;k++){
                           ExamPaperAnswerSheetRecordModel *emodel = examRecords[k];
                           if([emodel.bigQuestionId isEqualToString:model.bigQuestionId]&&[questionId isEqualToString:emodel.questionId]){
                               emodel.correct = correct;
                               emodel.isDone = isDone;
                               [earr replaceObjectAtIndex:k withObject:emodel];
                               break;
                           }
                       }
                       listModel.examRecords = earr;
                       [answerArr replaceObjectAtIndex:j withObject:listModel];
                   }
                   amodel.answers = answerArr;
                   [self.answerSheetArr replaceObjectAtIndex:i withObject:amodel];
                   break;
               }
           }
           
           if(self.testType == 0 || self.testType == 1){
               self.countLabel.text = [NSString stringWithFormat:@"%ld/%ld",self.answerArr.count,self.totalCount];
           }
           if((self.answerArr.count > 0) && (self.answerArr.count == self.totalCount)){
               self.isFinish = YES;
               [self.commitBtn setTitleColor:UIColorHex(0xFE4A4B) forState:UIControlStateNormal];
           }
            
           if(self.testType == 1){
               //考试 延时1s 去到下一个
               dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                   if(model.isParallel&&[model.isParallel isEqualToString:@"1"]){
                        //是否是阅读的最后一个
                       if(model.read_question_index >= model.questionList.count-1){
                           if(self.sel_index+1<self.dataArr.count){
                               [self.collectionView setContentOffset:CGPointMake((self.sel_index+1)*self.currentAvailableWidth,0) animated:YES];
                           }
                       }else{
                           //滑动阅读小问题
                           if(self.readCell){
                               [self.readCell scrollToNext:model.read_question_index+1];
                           }
                       }
                   }else{
                       //不是阅读题就滑动到下一个
                       if(self.sel_index+1<self.dataArr.count){
                           [self.collectionView setContentOffset:CGPointMake((self.sel_index+1)*self.currentAvailableWidth,0) animated:YES];
                       }
                   }
               });
           }
           
       }
            
      
    }
    
    if([manager isEqual:self.answerSheetManager]){
        NSDictionary *dic = manager.response.content;
        NSString *count = [NSString stringWithFormat:@"%@",dic[@"data"][@"count"]];
        NSArray *arr = [ExamPaperAnswerSheetModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"list"]];
        [self.answerSheetArr removeAllObjects];
        self.listen_count = 0;
        NSInteger q_j = 0;//问题几 除了听力题外 累计
        for(NSInteger i = 0;i < arr.count;i++){
            ExamPaperAnswerSheetModel *model = arr[i];
            NSArray *answers = [ExamPaperAnswerSheetListModel mj_objectArrayWithKeyValuesArray:model.answers];
            NSMutableArray *answerArr = [NSMutableArray array];
            for(NSInteger j = 0;j < answers.count;j++){
                ExamPaperAnswerSheetListModel *listModel = answers[j];
                if([model.typeId isEqualToString:@"33"]){
                    listModel.name = [NSString stringWithFormat:@"问题%ld",j+1];
                }else{
                    if([self.paperModel.grade isEqualToString:@"30"]){
                        //n3 语法题从问题1开始
                        if([model.typeId isEqualToString:@"46"]){
                            listModel.name = [NSString stringWithFormat:@"问题%ld",j+1];
                            q_j = j+1;
                        }else{
                            q_j++;
                            listModel.name = [NSString stringWithFormat:@"问题%ld",q_j];
                        }
                    }else{
                        q_j++;
                        listModel.name = [NSString stringWithFormat:@"问题%ld",q_j];
                    }
                }
                NSArray *examRecords = [ExamPaperAnswerSheetRecordModel mj_objectArrayWithKeyValuesArray:listModel.examRecords];
                NSMutableArray *earr = [NSMutableArray array];
                for(NSInteger k = 0;k < examRecords.count;k++){
                    ExamPaperAnswerSheetRecordModel *emodel = examRecords[k];
                    [earr addObject:emodel];
                }
                listModel.examRecords = earr;
                if(listModel.examRecords.count > 0){
                    [answerArr addObject:listModel];
                }
                if([model.typeId isEqualToString:@"33"]){
                    self.listen_count = self.listen_count+earr.count;
                }
            }
            model.answers = answerArr;
            if(answerArr.count > 0){
                if(self.testType == 1){
                    if([model.typeId isEqualToString:@"33"]){
                        [self.listenAnswerSheetArr addObject:model];
                    }else{
                        [self.answerSheetArr addObject:model];
                    }
                }else{
                    [self.answerSheetArr addObject:model];
                }
            }
        }
        if(self.testType == 1){
            self.answerSheetCount = [NSString stringWithFormat:@"%ld",count.integerValue-self.listen_count];
        }else{
            self.answerSheetCount = [NSString stringWithFormat:@"%ld",count.integerValue];
        }
        if(self.answerSheetManager.isFirst == NO){
            [self showAnswerSheet];
        }
    }
    
    
    if([manager isEqual:self.resultAnswerSheetManager]){
        NSDictionary *dic = manager.response.content;
        NSString *count = [NSString stringWithFormat:@"%@",dic[@"data"][@"count"]];
        NSArray *arr = [ExamPaperAnswerSheetModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"list"]];
        [self.answerSheetArr removeAllObjects];
        NSInteger q_j = 0;//问题几 除了听力题外 累计
        for(NSInteger i = 0;i < arr.count;i++){
            ExamPaperAnswerSheetModel *model = arr[i];
            NSArray *answers = [ExamPaperAnswerSheetListModel mj_objectArrayWithKeyValuesArray:model.answers];
            NSMutableArray *answerArr = [NSMutableArray array];
            for(NSInteger j = 0;j < answers.count;j++){
                ExamPaperAnswerSheetListModel *listModel = answers[j];
                if([model.typeId isEqualToString:@"33"]){
                    listModel.name = [NSString stringWithFormat:@"问题%ld",j+1];
                }else{
                    if([self.paperModel.grade isEqualToString:@"30"]){
                        //n3 语法题从问题1开始
                        if([model.typeId isEqualToString:@"46"]){
                            listModel.name = [NSString stringWithFormat:@"问题%ld",j+1];
                            q_j = j+1;
                        }else{
                            q_j++;
                            listModel.name = [NSString stringWithFormat:@"问题%ld",q_j];
                        }
                    }else{
                        q_j++;
                        listModel.name = [NSString stringWithFormat:@"问题%ld",q_j];
                    }
                }
                NSArray *examRecords = [ExamPaperAnswerSheetRecordModel mj_objectArrayWithKeyValuesArray:listModel.examRecords];
                NSMutableArray *earr = [NSMutableArray array];
                for(NSInteger k = 0;k < examRecords.count;k++){
                    ExamPaperAnswerSheetRecordModel *emodel = examRecords[k];
                    [earr addObject:emodel];
                }
                listModel.examRecords = earr;
                [answerArr addObject:listModel];
            }
            model.answers = answerArr;
            [self.answerSheetArr addObject:model];
        }
        self.answerSheetCount = [NSString stringWithFormat:@"%ld",count.integerValue];
        if(self.resultAnswerSheetManager.isFirst == NO){
            [self showAnswerSheet];
        }
    }
    
    if([manager isEqual:self.collectSaveManager]){
        NSDictionary *dic = manager.response.content;
        if(dic[@"message"]){
            [JPProgressHUD showMsg:dic[@"message"]];
        }
        NSString *collectId = [NSString stringWithFormat:@"%@",dic[@"data"][@"collectId"]];
        self.selQuestionModel.collect = @"1";
        self.selQuestionModel.collectId = collectId;
        self.collectBtn.selected = YES;
        if(self.selQuestionModel.isParallel&&[self.selQuestionModel.isParallel isEqualToString:@"1"]){
            if(self.big_question_index < self.dataArr.count){
                ExamQuestionModel *model = self.dataArr[self.big_question_index];
                NSMutableArray *qarr = [NSMutableArray arrayWithArray:model.questionList];
                for(NSInteger i = 0;i < model.questionList.count;i++){
                    ExamQuestionModel *model2 = model.questionList[i];
                    if([model2.questionId isEqualToString:self.selQuestionModel.questionId]){
                        [qarr replaceObjectAtIndex:i withObject:self.selQuestionModel];
                        break;
                    }
                }
                model.questionList = qarr;
                [self.dataArr replaceObjectAtIndex:self.big_question_index withObject:model];
            }
        }else{
            if(self.big_question_index < self.dataArr.count){
                [self.dataArr replaceObjectAtIndex:self.big_question_index withObject:self.selQuestionModel];
            }
        }
    }
    
    if([manager isEqual:self.collectDeleteManager]){
        NSDictionary *dic = manager.response.content;
        if(dic[@"message"]){
            [JPProgressHUD showMsg:dic[@"message"]];
        }
        self.selQuestionModel.collect = @"0";
        self.selQuestionModel.collectId = @"";
        self.collectBtn.selected = NO;
        if(self.selQuestionModel.isParallel&&[self.selQuestionModel.isParallel isEqualToString:@"1"]){
            if(self.big_question_index < self.dataArr.count){
                ExamQuestionModel *model = self.dataArr[self.big_question_index];
                NSMutableArray *qarr = [NSMutableArray arrayWithArray:model.questionList];
                for(NSInteger i = 0;i < model.questionList.count;i++){
                    ExamQuestionModel *model2 = model.questionList[i];
                    if([model2.questionId isEqualToString:self.selQuestionModel.questionId]){
                        [qarr replaceObjectAtIndex:i withObject:self.selQuestionModel];
                        break;
                    }
                }
                model.questionList = qarr;
                [self.dataArr replaceObjectAtIndex:self.big_question_index withObject:model];
            }
        }else{
            if(self.big_question_index < self.dataArr.count){
                [self.dataArr replaceObjectAtIndex:self.big_question_index withObject:self.selQuestionModel];
            }
        }
    }
    
    if([manager isEqual:self.commitExamManager]){
        NSDictionary *dic = manager.response.content;
        if(self.commitHub){
            [self.commitHub removeFromSuperview];
        }
        if(dic[@"message"]){
            [JPProgressHUD showMsg:dic[@"message"]];
        }
        NSString *isexam = @"1";//试卷
        if(![self.commitExamManager.ptId isEqualToString:@""]){
            isexam = @"0";
        }
        [[NSNotificationCenter defaultCenter]postNotificationName:@"CommitExamSuccess" object:isexam];
        
        //刷新当前页面 查看答案
        self.onlyErrorBtn.hidden = NO;
        self.big_question_index = -1;
        self.totalCount = 0;
        self.commitBtn.hidden = YES;
        if(self.avPlayer){
            [self.avPlayer pause];
            self.avPlayer = nil;
        }
        if(self.typeModel&&[self.typeModel.typeId isEqualToString:@"33"]){
            //听力
            self.listenImageView.hidden = NO;
            [self stopVoiceAnimation];
        }else{
            if(self.testType == 1&&self.isHaveListen == YES){
                //考试有听力
                self.listenImageView.hidden = NO;
                [self stopVoiceAnimation];
            }else{
                self.listenImageView.hidden = YES;
            }
        }
        if(self.countDownView){
            self.countDownView.hidden = YES;
        }
        if(self.timer){
            [self.timer invalidate];
            self.timer = nil;
        }
        
        if(self.listenTimer){
            [self.listenTimer invalidate];
            self.listenTimer = nil;
        }
        [self.answerArr removeAllObjects];
        [self.dataArr removeAllObjects];
        [self.answerSheetArr removeAllObjects];
        if([isexam isEqualToString:@"1"]){
            self.testType = 2;
        }else{
            self.testType = 3;
        }
        self.loadingHub = [JPProgressHUD showLoadingInView:self.view];
        [self loadQuestionResultDataWithCorrect:@""];

    }
}

-(void)managerCallDLDLAPIDidFailed:(JPAPIBaseManager *)manager{
    if([manager isEqual:self.answerSheetManager]){
        if(self.answerSheetManager.isFirst == NO){
            [self showAnswerSheet];
        }
    }
    
    if([manager isEqual:self.resultAnswerSheetManager]){
        if(self.resultAnswerSheetManager.isFirst == NO){
            [self showAnswerSheet];
        }
    }
    
    if([manager isEqual:self.commitExamManager]){
        if(self.commitHub){
            [self.commitHub removeFromSuperview];
        }
    }
    
    if([manager isEqual:self.questionManager]||[manager isEqual:self.questionResultManager]){
        if(self.loadingHub){
            [self.loadingHub removeFromSuperview];
        }
    }
    
    if(manager.errorMessage){
        [JPProgressHUD showMsg:manager.errorMessage];
    }
}
 
#pragma mark - 专项有做过题的记录 直接滑动到最后一个做过的题后面一个题
-(void)scrollToLastRecoredWithFinishIndex:(NSInteger)finish_index{
    if(self.dataArr.count > 0){
        ExamQuestionModel *model0 = self.dataArr[0];
        self.big_question_index = 0;
        if(self.testType == 0){
            NSInteger s_index = (finish_index > 0)?(finish_index-1):0;
            if(finish_index > 0){
                if(s_index+1 < self.dataArr.count){
                    //有下一个 滑动到下一个
                    if(model0.isParallel&&[model0.isParallel isEqualToString:@"1"]){
                        //阅读题的时候看有没有下一个小题
                        if(self.answerArr.count > 0){
                            NSString *questionId = self.answerArr[self.answerArr.count-1];
                            NSInteger question_index = -1;
                            for(NSInteger i = 0;i < model0.questionList.count;i++){
                                ExamQuestionModel *questionModel = model0.questionList[i];
                                if([questionModel.questionId isEqualToString:questionId]){
                                    question_index = i;
                                    break;
                                }
                            }
                            if(question_index == model0.questionList.count-1){
                                //没有下一题
                                self.big_question_index = s_index+1;
                                model0 = self.dataArr[self.big_question_index];
                                [self.collectionView setContentOffset:CGPointMake(self.big_question_index*self.currentAvailableWidth,0) animated:NO];
                            }else{
                                //有下一题
                                self.big_question_index = s_index;
                                model0 = self.dataArr[self.big_question_index];
                                [self.collectionView setContentOffset:CGPointMake(self.big_question_index*self.currentAvailableWidth,0) animated:NO];
                            }
                        }else{
                            self.big_question_index = s_index+1;
                            model0 = self.dataArr[self.big_question_index];
                            [self.collectionView setContentOffset:CGPointMake(self.big_question_index*self.currentAvailableWidth,0) animated:NO];
                        }
                    }else{
                        self.big_question_index = s_index+1;
                        model0 = self.dataArr[self.big_question_index];
                        [self.collectionView setContentOffset:CGPointMake(self.big_question_index*self.currentAvailableWidth,0) animated:NO];
                    }
                }else{
                    //没有的话就停留在当前
                    if(s_index < self.dataArr.count){
                        self.big_question_index = s_index;
                        model0 = self.dataArr[self.big_question_index];
                        [self.collectionView setContentOffset:CGPointMake(self.big_question_index*self.currentAvailableWidth,0) animated:NO];
                    }
                }
            }
        }
        
        if(model0.isParallel&&[model0.isParallel isEqualToString:@"1"]){
            if(model0.questionList.count > 0){
                __block ExamQuestionModel *model2 = model0.questionList[0];
                if(self.testType == 0){
                    if(self.answerArr.count > 0){
                        NSString *questionId = self.answerArr[self.answerArr.count-1];
                        NSInteger q_index = -1;
                        for(NSInteger i = 0;i < model0.questionList.count;i++){
                            ExamQuestionModel *questionModel = model0.questionList[i];
                            if([questionModel.questionId isEqualToString:questionId]){
                                q_index = i;
                                break;
                            }
                        }
                        if((q_index > -1)&&(q_index < model0.questionList.count)){
                            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.big_question_index inSection:0];
                                if(indexPath.item < self.dataArr.count){
                                    ExamQuestionReadCollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
                                    if ([cell isKindOfClass:[ExamQuestionReadCollectionViewCell class]]) {
                                        if(q_index+1 < model0.questionList.count){
                                            //下一个
                                            model2 = model0.questionList[q_index+1];
                                            [cell scrollToNext:q_index+1];
                                        }else{
                                            //当前
                                            model2 = model0.questionList[q_index];
                                            [cell scrollToNext:q_index];
                                        }
                                        self.selQuestionModel = model2;
                                        if([model2.collect isEqualToString:@"1"]){
                                            self.collectBtn.selected = YES;
                                        }else{
                                            self.collectBtn.selected = NO;
                                        }
                                    }
                                }
                            });
                        }else{
                            self.selQuestionModel = model2;
                            if([model2.collect isEqualToString:@"1"]){
                                self.collectBtn.selected = YES;
                            }else{
                                self.collectBtn.selected = NO;
                            }
                        }
                    }else{
                        self.selQuestionModel = model2;
                        if([model2.collect isEqualToString:@"1"]){
                            self.collectBtn.selected = YES;
                        }else{
                            self.collectBtn.selected = NO;
                        }
                    }
                }else{
                    
                    self.selQuestionModel = model2;
                    if([model2.collect isEqualToString:@"1"]){
                        self.collectBtn.selected = YES;
                    }else{
                        self.collectBtn.selected = NO;
                    }
                }
                
            }
        }else if([model0.type isEqualToString:@"36"]){
            if([model0.collect isEqualToString:@"1"]){
                self.collectBtn.selected = YES;
            }else{
                self.collectBtn.selected = NO;
            }
        }else{
            self.selQuestionModel = model0;
            if([model0.collect isEqualToString:@"1"]){
                self.collectBtn.selected = YES;
            }else{
                self.collectBtn.selected = NO;
            }
        }
    }
    if(self.testType == 2||self.testType == 3){
        [self.collectionView setContentOffset:CGPointMake(0,0) animated:NO];
    }
}

#pragma mark - 模型赋值
-(void)changeModel:(ExamQuestionModel*)questionModel withQmodel:(ExamQuestionModel*)qmodel{
    questionModel.questionId = qmodel.questionId;
    questionModel.paperId = qmodel.paperId;
    questionModel.content = qmodel.content;
    questionModel.path = qmodel.path;
    questionModel.score = qmodel.score;
    questionModel.answer = qmodel.answer;
    questionModel.analysis = qmodel.analysis;
    questionModel.type = qmodel.type;
    questionModel.sortId = qmodel.sortId;
    questionModel.createTime = qmodel.createTime;
    questionModel.updateTime = qmodel.updateTime;
    questionModel.bigQuestionId = qmodel.bigQuestionId;
    questionModel.bigQuestionContent = qmodel.bigQuestionContent;
    questionModel.questionOptions = qmodel.questionOptions;
    questionModel.correct = qmodel.correct;
    questionModel.doAnswer = qmodel.doAnswer;
    questionModel.collect = qmodel.collect;
    questionModel.collectId = qmodel.collectId;
    questionModel.isDone = qmodel.isDone;
    questionModel.examCenter = qmodel.examCenter;
    questionModel.paperName = qmodel.paperName;
    questionModel.bsort = qmodel.bsort;
    questionModel.lsort = qmodel.lsort;
    questionModel.isSelect = qmodel.isSelect;
    questionModel.isParallel = qmodel.isParallel;
    questionModel.questionList = qmodel.questionList;
    questionModel.hearingFile = qmodel.hearingFile;
    questionModel.name = qmodel.name;
    questionModel.scroll_index = qmodel.scroll_index;
}


#pragma mark - 移除 定时器 观察等
-(void)remove{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    
    if(self.listenTimer){
        [self.listenTimer invalidate];
        self.listenTimer = nil;
    }
    
    if(self.avPlayer){
        [self.avPlayer pause];
        self.avPlayer = nil;
    }
}


-(void)dealloc{
    
    [self remove];
    
}


@end
